package com.ruoyi.factory.taxShare.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;

import com.ruoyi.factory.common.utils.IdGen;
import com.ruoyi.factory.costControl.service.CostHandleService;
import com.ruoyi.factory.qylock.utils.OkHttpUtils;
import com.ruoyi.factory.taxShare.domain.*;
import com.ruoyi.factory.taxShare.mapper.*;
import com.ruoyi.factory.taxShare.service.TaxReceiptService;

import com.ruoyi.factory.taxShare.utils.OpenApiUtils;
import com.ruoyi.factory.taxShare.utils.PurchaseParaUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName TaxReceiptService
 * @Description: TODO 税务共享进项业务
 * @Author LiuJiaji
 * @Date 2023/11/24 14:24
 **/
@Service
public class TaxReceiptServiceImpl implements TaxReceiptService {

    protected final Logger logger = LoggerFactory.getLogger(TaxReceiptServiceImpl.class);

    @Autowired
    private PurchaseMapper purchaseMapper;

    @Autowired
    private PurTaxMapper purTaxMapper;

    @Autowired
    private YfdTaxMapper yfdTaxMapper;

    @Autowired
    private YfyfTaxMapper yfyfTaxMapper;

    @Autowired
    private BdMaterialMapper bdMaterialMapper;

    @Autowired
    private TranTaxMapper tranTaxMapper;

    @Autowired
    private CostHandleService costHandleService;

    @Value("${tax.sendUrl}")
    private String sendUrl;

    /**
     * @author: Liujiaji
     * @date: 2023/11/24 14:47
     * @description: TODO 采购入库单查询
     * @param: [param, request]
     * @return: com.ruoyi.common.core.domain.AjaxResult
     **/
    public AjaxResult findPurchase(JSONObject param, HttpServletRequest request){
        //设置查询条件
        logger.info("税务平台请求查询erp采购入库单入参: "+param);
        PurchaseHead parm = new PurchaseHead();
        parm.setHsdwdm(param.optString("hsdwdm"));
        parm.setZdydjbh(param.optString("zdydjbh"));
        parm.setDjrqq(param.optString("djrqq"));
        parm.setDjrqz(param.optString("djrqz"));
        parm.setCrklx(param.optString("crklx"));
        parm.setGysmc(param.optString("gysmc"));

        List<PurchaseHead> list = purchaseMapper.findAll(parm);
        //处理日期
        for (PurchaseHead purchaseHead : list) {
            String djrq = purchaseHead.getDjrq();
            if(StringUtils.isNotBlank(djrq)){
                String substring = djrq.replace("-", "").substring(0, 6);
                purchaseHead.setAutoCheckPeriod(substring);
            }
        }

        //分页查询
        Integer page = 1;
        Integer rows = 0;
        if(StringUtils.isNotEmpty(param.optString("pageIndex")) && StringUtils.isNotEmpty(param.optString("pageSize"))){
            page = Integer.valueOf(param.optString("pageIndex"));
            rows  = Integer.valueOf(param.optString("pageSize"));
            if(page<=0) page = 1;
            if(rows<=0) rows = list.size();
        }else{
            rows = list.size();
        }
        List<PurchaseHead> all  = new ArrayList<>();
        int size=list.size();
        int pageStart=page==1?0:(page-1)*rows;//截取的开始位置
        int pageEnd=size<page*rows?size:page*rows;//截取的结束位置
        if(size>pageStart){
            all =list.subList(pageStart, pageEnd);
        }
        //总页数
        if(0!=list.size()){
            int totalPage=list.size()/rows;
        }

        //处理返回结果
        JSONObject rejson = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        for (PurchaseHead purchaseHead : all) {
            jsonArray.add(purchaseHead);
        }
        rejson.put("totalRecordCount", list.size());
        rejson.put("list",jsonArray);
        //应该设置可以批量接收数据
        String remoteAddr = request.getRemoteAddr();
        String localAddr = request.getLocalAddr();

        logger.info("税务平台请求查询erp采购入库单返参: "+rejson);
        if(StringUtils.isEmpty(localAddr)){
            return new AjaxResult(500,"失败");
        }
        return new AjaxResult(0,"成功",rejson);
    }

    /**
     * @author: Liujiaji
     * @date: 2023/12/6 09:45
     * @description: TODO 采购发票查询
     * @param: [param, param2]
     * @return: com.ruoyi.common.core.domain.AjaxResult
     **/
    public AjaxResult findPurTax(JSONObject param, HttpServletRequest request){
        //设置查询条件
        String uuid = IdGen.uuid();
        logger.info(uuid+"税务平台请求查询erp采购发票单入参: "+param);
        PurTaxHead parm = new PurTaxHead();
        parm.setHsdwdm(param.optString("hsdwdm"));
        parm.setZdydjbh(param.optString("zdydjbh"));
        //校验单据起止日期
        if(StringUtils.isBlank(param.optString("djrqq")) || StringUtils.isBlank(param.optString("djrqz"))) return new AjaxResult(500,"查询单据起止日期不可为空");
        parm.setDjrqq(param.optString("djrqq"));
        parm.setDjrqz(param.optString("djrqz"));
        parm.setGysmc(param.optString("gysmc"));

        List<PurTaxHead> list = purTaxMapper.findAll(parm);
        //设置金税格式日期
        for (PurTaxHead purTaxHead : list) {
            String djrq = purTaxHead.getDjrq();
            if(StringUtils.isNotBlank(djrq)){
                String substring = djrq.replace("-", "").substring(0, 6);
                purTaxHead.setAutoCheckPeriod(substring);
            }
        }

        //分页查询
        Integer page = 1;
        Integer rows = 0;
        if(StringUtils.isNotEmpty(param.optString("pageIndex")) && StringUtils.isNotEmpty(param.optString("pageSize"))){
            page = Integer.valueOf(param.optString("pageIndex"));
            rows  = Integer.valueOf(param.optString("pageSize"));
            if(page<=0) page = 1;
            if(rows<=0) rows = list.size();
        }else{
            rows = list.size();
        }
        List<PurTaxHead> all  = new ArrayList<>();
        int size=list.size();
        int pageStart=page==1?0:(page-1)*rows;//截取的开始位置
        int pageEnd=size<page*rows?size:page*rows;//截取的结束位置
        if(size>pageStart){
            all =list.subList(pageStart, pageEnd);
        }
        //总页数
        if(0!=list.size()){
            int totalPage=list.size()/rows;
        }

        //处理返回结果
        JSONObject rejson = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        for (PurTaxHead purTaxHead : all) {
            JSONObject jsonObject = JSONObject.fromObject(purTaxHead);
            jsonArray.add(jsonObject);
        }
        rejson.put("totalRecordCount", list.size());
        rejson.put("list",jsonArray);
        //应该设置可以批量接收数据
        String remoteAddr = request.getRemoteAddr();
        String localAddr = request.getLocalAddr();

        logger.info(uuid+"税务平台请求查询erp采购发票单返参: "+rejson);
        if(StringUtils.isEmpty(localAddr)){
            return new AjaxResult(500,"失败");
        }
        return new AjaxResult(0,"成功",rejson);
    }

    public AjaxResult findYfdTax(JSONObject param, HttpServletRequest request){
        //设置查询条件
        String uuid = IdGen.uuid();
        logger.info(uuid+"税务平台请求查询erp应付单入参: "+param);
        YfdTaxHead parm = new YfdTaxHead();
        parm.setHsdwdm(param.optString("hsdwdm"));
        parm.setZdydjbh(param.optString("zdydjbh"));
        //校验单据起止日期
        if(StringUtils.isBlank(param.optString("djrqq")) || StringUtils.isBlank(param.optString("djrqz"))) return new AjaxResult(500,"查询单据起止日期不可为空");
        parm.setDjrqq(param.optString("djrqq"));
        parm.setDjrqz(param.optString("djrqz"));
        parm.setGysmc(param.optString("gysmc"));

        List<YfdTaxHead> list = yfdTaxMapper.findAll(parm);
        //设置金税格式日期
        for (YfdTaxHead yfdTaxHead : list) {
            String djrq = yfdTaxHead.getDjrq();
            if(StringUtils.isNotBlank(djrq)){
                String substring = djrq.replace("-", "").substring(0, 6);
                yfdTaxHead.setAutoCheckPeriod(substring);
            }
        }

        //分页查询
        Integer page = 1;
        Integer rows = 0;
        if(StringUtils.isNotEmpty(param.optString("pageIndex")) && StringUtils.isNotEmpty(param.optString("pageSize"))){
            page = Integer.valueOf(param.optString("pageIndex"));
            rows  = Integer.valueOf(param.optString("pageSize"));
            if(page<=0) page = 1;
            if(rows<=0) rows = list.size();
        }else{
            rows = list.size();
        }
        List<YfdTaxHead> all  = new ArrayList<>();
        int size=list.size();
        int pageStart=page==1?0:(page-1)*rows;//截取的开始位置
        int pageEnd=size<page*rows?size:page*rows;//截取的结束位置
        if(size>pageStart){
            all =list.subList(pageStart, pageEnd);
        }
        //总页数
        if(0!=list.size()){
            int totalPage=list.size()/rows;
        }

        //处理返回结果
        JSONObject rejson = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        for (YfdTaxHead yfdTaxHead : all) {
            JSONObject jsonObject = JSONObject.fromObject(yfdTaxHead);
            jsonArray.add(jsonObject);
        }
        rejson.put("totalRecordCount", list.size());
        rejson.put("list",jsonArray);
        //应该设置可以批量接收数据
        String remoteAddr = request.getRemoteAddr();
        String localAddr = request.getLocalAddr();

        logger.info(uuid+"税务平台请求查询erp应付单返参: "+rejson);
        if(StringUtils.isEmpty(localAddr)){
            return new AjaxResult(500,"失败");
        }
        return new AjaxResult(0,"成功",rejson);
    }

    /**
     * @author: Liujiaji
     * @date: 2023/12/6 09:45
     * @description: TODO 应付运费发票查询
     * @param: [param, param2]
     * @return: com.ruoyi.common.core.domain.AjaxResult
     **/
    public AjaxResult findYfyfTax(JSONObject param, HttpServletRequest request){
        //设置查询条件
        String uuid = IdGen.uuid();
        logger.info(uuid+"税务平台请求查询erp应付运费发票单入参: "+param);
        YfyfTaxHead parm = new YfyfTaxHead();
        parm.setHsdwdm(param.optString("hsdwdm"));
        parm.setZdydjbh(param.optString("zdydjbh"));
        //校验单据起止日期
        if(StringUtils.isBlank(param.optString("djrqq")) || StringUtils.isBlank(param.optString("djrqz"))) return new AjaxResult(500,"查询单据起止日期不可为空");
        parm.setDjrqq(param.optString("djrqq"));
        parm.setDjrqz(param.optString("djrqz"));
        parm.setGysmc(param.optString("gysmc"));

        List<YfyfTaxHead> list = yfyfTaxMapper.findAll(parm);
        //设置金税格式日期
        for (YfyfTaxHead yfyfTaxHead : list) {
            String djrq = yfyfTaxHead.getDjrq();
            if(StringUtils.isNotBlank(djrq)){
                String substring = djrq.replace("-", "").substring(0, 6);
                yfyfTaxHead.setAutoCheckPeriod(substring);
            }
        }

        //分页查询
        Integer page = 1;
        Integer rows = 0;
        if(StringUtils.isNotEmpty(param.optString("pageIndex")) && StringUtils.isNotEmpty(param.optString("pageSize"))){
            page = Integer.valueOf(param.optString("pageIndex"));
            rows  = Integer.valueOf(param.optString("pageSize"));
            if(page<=0) page = 1;
            if(rows<=0) rows = list.size();
        }else{
            rows = list.size();
        }
        List<YfyfTaxHead> all  = new ArrayList<>();
        int size=list.size();
        int pageStart=page==1?0:(page-1)*rows;//截取的开始位置
        int pageEnd=size<page*rows?size:page*rows;//截取的结束位置
        if(size>pageStart){
            all =list.subList(pageStart, pageEnd);
        }
        //总页数
        if(0!=list.size()){
            int totalPage=list.size()/rows;
        }

        //处理返回结果
        JSONObject rejson = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        for (YfyfTaxHead yfyfTaxHead : all) {
            JSONObject jsonObject = JSONObject.fromObject(yfyfTaxHead);
            jsonArray.add(jsonObject);
        }
        rejson.put("totalRecordCount", list.size());
        rejson.put("list",jsonArray);
        //应该设置可以批量接收数据
        String remoteAddr = request.getRemoteAddr();
        String localAddr = request.getLocalAddr();

        logger.info(uuid+"税务平台请求查询erp应付运费发票单返参: "+rejson);
        if(StringUtils.isEmpty(localAddr)){
            return new AjaxResult(500,"失败");
        }
        return new AjaxResult(0,"成功",rejson);
    }



    public AjaxResult addPurchaseBill(JSONObject param, HttpServletRequest request){
        logger.info("税务平台请求erp采购配单入参: "+param);
        JSONArray jsonArray = param.optJSONArray("item");
        //整理请求erp单据查询参数
        String zdydjbh = "";
        String hsdwdm = param.optString("hsdwdm");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long l = System.currentTimeMillis();
        Calendar instance = Calendar.getInstance();
        instance.setTimeInMillis(l);
        instance.add(Calendar.YEAR,-2);
        String zdate = sdf.format(l);
        String qdate = sdf.format(instance.getTimeInMillis());
        String prarmdate = qdate+"~"+zdate;
        JSONArray jsonAr = new JSONArray();
        for (Object o : jsonArray) {
            JSONObject jsonObject = JSONObject.fromObject(o);
            zdydjbh = jsonObject.optString("zdydjbh");
            jsonAr.add(zdydjbh);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("dbilldate",prarmdate);
        jsonObject.put("pk_org",hsdwdm);
        jsonObject.put("vbillcode",jsonAr);

        String params = jsonObject.toString();
        //将 s 传到 erp中
        String apiuri = "nccloud/api/ic/purchasein/commonquery";
        String result = OpenApiUtils.openApiSend(apiuri, params,"采购入库单查询");
        //整理返回参数
        JSONObject reso = JSONObject.fromObject(result);
        JSONArray jsonArray1 = reso.optJSONArray("data");
        if(null == jsonArray1){
            return new AjaxResult(500,"失败",zdydjbh+"此单据号不存在");
        }else{
            for (int i = 0; i < jsonArray1.size(); i++) {
                if(i != 0){
                    JSONObject jsonObject1 = jsonArray1.getJSONObject(i);
                    JSONArray jsonArray2 = jsonObject1.optJSONArray("ic.PurchaseInBodyVO");
                    JSONObject jsonObject2 = jsonArray1.getJSONObject(0);
                    JSONArray jsonArray3 = jsonObject2.optJSONArray("ic.PurchaseInBodyVO");
                    for (Object o : jsonArray2) {
                        jsonArray3.add(o);
                    }
                    jsonArray1.remove(i);
                }
            }
        }
        //请求采购发票生成
        //匹配采购发票入参
        //通过openapi保存
//        savecgfpapi(jsonArray1,param);
//        if(true) return null;
        String params2 = matchparam(jsonArray1,param);
//        String params3 = matchparam2(jsonArray1,param);
        String apiuri2 = "nccloud/api/pu/invoicemanage/baseinfo/setInvoicebaseinfo";
//        String apiuri2 = "nccloud/api/uapbd/customermanage/baseinfo/getCustomerbaseinfo";
        String s1 = OpenApiUtils.openApiSend(apiuri2, params2,"采购发票新增");
//        s1="{\"success\":true,\"data\":\"{\\\"pageid\\\":\\\"400401600_card\\\",\\\"head\\\":{\\\"card_head\\\":{\\\"areaType\\\":\\\"form\\\",\\\"rows\\\":[{\\\"values\\\":{\\\"pk_org_v\\\":{\\\"value\\\":\\\"0001A110000000002TJ9\\\"},\\\"ctrantypeid\\\":{\\\"value\\\":\\\"0001A110000000002BLW\\\"},\\\"finvoiceclass\\\":{\\\"value\\\":\\\"0\\\"},\\\"dbilldate\\\":{\\\"value\\\":\\\"2024-02-28 15:19:51\\\"},\\\"darrivedate\\\":{\\\"value\\\":\\\"2024-02-28 15:19:51\\\"},\\\"pk_supplier\\\":{\\\"value\\\":\\\"1001A110000000000Y99\\\"},\\\"pk_supplier_v\\\":{\\\"value\\\":\\\"1001A110000000000Y99\\\"},\\\"corigcurrencyid\\\":{\\\"value\\\":\\\"1002Z0100000000001K1\\\"},\\\"pk_bizpsn\\\":{\\\"value\\\":\\\"0001A11000000000QJYR\\\"},\\\"pk_dept_v\\\":{\\\"value\\\":\\\"0001A91000000006WZBD\\\"},\\\"pk_stockorg_v\\\":{\\\"value\\\":\\\"0001A110000000002TJ9\\\"},\\\"pk_paytosupplier\\\":{\\\"value\\\":\\\"1001A110000000000Y99\\\"},\\\"pk_balatype\\\":{\\\"value\\\":\\\"\\\"},\\\"binitial\\\":{\\\"value\\\":false},\\\"ntotalastnum\\\":{},\\\"ntotalorigmny\\\":{},\\\"vmemo\\\":{\\\"value\\\":\\\"RFXC-CG-A-202401-095\\\"},\\\"pk_org\\\":{\\\"value\\\":\\\"0001A110000000002TJA\\\"},\\\"vbillcode\\\":{\\\"value\\\":\\\"RF67991566\\\"},\\\"bvirtual\\\":{},\\\"bfee\\\":{},\\\"pk_freecust\\\":{},\\\"supplierphone\\\":{},\\\"pk_dept\\\":{\\\"value\\\":\\\"1001A110000000002RCB\\\"},\\\"pk_stockorg\\\":{\\\"value\\\":\\\"0001A110000000002TJA\\\"},\\\"ts\\\":{},\\\"dmakedate\\\":{},\\\"creator\\\":{\\\"value\\\":\\\"1001A11000000009I64N\\\"},\\\"approver\\\":{\\\"value\\\":\\\"1001A11000000009I64N\\\"},\\\"vtrantypecode\\\":{\\\"value\\\":\\\"25-01\\\"},\\\"taudittime\\\":{},\\\"creationtime\\\":{\\\"value\\\":\\\"2024-02-28 15:19:51\\\"},\\\"pk_frozenuser\\\":{},\\\"modifier\\\":{},\\\"tfrozentime\\\":{},\\\"modifiedtime\\\":{},\\\"pk_bankaccbas\\\":{},\\\"vbankaccount\\\":{},\\\"ccurrencyid\\\":{\\\"value\\\":\\\"1002Z0100000000001K1\\\"},\\\"nexchangerate\\\":{\\\"value\\\":\\\"1\\\"},\\\"vfrozenreason\\\":{},\\\"bfrozen\\\":{},\\\"pk_busitype\\\":{\\\"value\\\":\\\"1001A110000000002LBB\\\"},\\\"pk_purchaseorg\\\":{\\\"value\\\":\\\"0001A110000000002TJA\\\"},\\\"pk_purchaseorg_v\\\":{\\\"value\\\":\\\"0001A110000000002TJ9\\\"},\\\"pk_payterm\\\":{},\\\"ftaxtypeflagh\\\":{\\\"value\\\":\\\"1\\\"},\\\"ntaxrateh\\\":{\\\"value\\\":\\\"13\\\"},\\\"vparentcode\\\":{},\\\"pk_parentinvoice\\\":{},\\\"ngroupexchgrate\\\":{\\\"value\\\":\\\"1\\\"},\\\"nglobalexchgrate\\\":{\\\"value\\\":\\\"1\\\"},\\\"vdef1\\\":{\\\"value\\\":\\\"\\\"},\\\"vdef2\\\":{},\\\"vdef3\\\":{},\\\"vdef4\\\":{},\\\"vdef5\\\":{},\\\"vdef6\\\":{\\\"value\\\":\\\"\\\"},\\\"vdef7\\\":{\\\"value\\\":\\\"RFXC-CG-A-202401-095\\\"},\\\"vdef8\\\":{},\\\"vdef9\\\":{},\\\"vdef10\\\":{\\\"value\\\":\\\"\\\"},\\\"vdef11\\\":{},\\\"vdef12\\\":{\\\"value\\\":\\\"24212000000008632727\\\"},\\\"vdef13\\\":{},\\\"vdef14\\\":{},\\\"vdef15\\\":{},\\\"vdef16\\\":{},\\\"vdef17\\\":{},\\\"vdef18\\\":{},\\\"vdef19\\\":{},\\\"vdef20\\\":{},\\\"bapflag\\\":{},\\\"pk_invoice\\\":{},\\\"fbillstatus\\\":{\\\"value\\\":\\\"0\\\"},\\\"pk_group\\\":{\\\"value\\\":\\\"0001A110000000000C8B\\\"},\\\"iprintcount\\\":{\\\"value\\\":\\\"0\\\"},\\\"csendcountryid\\\":{\\\"value\\\":\\\"0001Z010000000079UJJ\\\"},\\\"crececountryid\\\":{\\\"value\\\":\\\"0001Z010000000079UJJ\\\"},\\\"ctaxcountryid\\\":{\\\"value\\\":\\\"0001Z010000000079UJJ\\\"},\\\"fbuysellflag\\\":{\\\"value\\\":\\\"2\\\"},\\\"btriatradeflag\\\":{\\\"value\\\":false},\\\"ctradewordid\\\":{\\\"value\\\":\\\"\\\"},\\\"bopptaxflag\\\":{\\\"value\\\":false},\\\"vvatcode\\\":{},\\\"vvendorvatcode\\\":{},\\\"finvoicetype\\\":{\\\"value\\\":\\\"0\\\"},\\\"saga_frozen\\\":{},\\\"saga_gtxid\\\":{},\\\"saga_btxid\\\":{},\\\"saga_status\\\":{},\\\"cextsrcsys\\\":{},\\\"billmaker\\\":{\\\"value\\\":\\\"1001A11000000009I64N\\\"},\\\"fratecategory\\\":{},\\\"vextsrcbillcode\\\":{},\\\"dratedate\\\":{},\\\"pseudocolumn\\\":{},\\\"vdef21\\\":{},\\\"vdef22\\\":{},\\\"vdef27\\\":{},\\\"vdef28\\\":{},\\\"vdef29\\\":{},\\\"vdef23\\\":{},\\\"vdef24\\\":{},\\\"vdef25\\\":{},\\\"vdef26\\\":{},\\\"vdef30\\\":{},\\\"vdef31\\\":{},\\\"vdef32\\\":{},\\\"vdef33\\\":{},\\\"vadjustreason\\\":{},\\\"vdef38\\\":{},\\\"vdef39\\\":{},\\\"vdef34\\\":{},\\\"vdef35\\\":{},\\\"vdef36\\\":{},\\\"vdef37\\\":{},\\\"vdef41\\\":{},\\\"vdef42\\\":{},\\\"vdef43\\\":{},\\\"vdef44\\\":{},\\\"vdef40\\\":{},\\\"dr\\\":{},\\\"vdef49\\\":{},\\\"vdef45\\\":{},\\\"vdef46\\\":{},\\\"vdef47\\\":{},\\\"vdef48\\\":{},\\\"vdef50\\\":{},\\\"csaleinvoiceid\\\":{},\\\"pk_paytosupplier_v\\\":{\\\"value\\\":\\\"1001A110000000000Y99\\\"},\\\"cratetype\\\":{}},\\\"status\\\":\\\"1\\\"}],\\\"areacode\\\":\\\"card_head\\\"}},\\\"body\\\":{\\\"card_body\\\":{\\\"areaType\\\":\\\"table\\\",\\\"rows\\\":[{\\\"status\\\":\\\"2\\\",\\\"isOptimized\\\":false,\\\"values\\\":{\\\"cfirstbid\\\":{\\\"value\\\":\\\"1001ZZ100000001TYMPT\\\"},\\\"naccumsettorigmny\\\":{},\\\"nnosubtax\\\":{\\\"value\\\":\\\"0\\\"},\\\"nastprice\\\":{\\\"value\\\":\\\"15.4900000000000\\\"},\\\"pk_material.name\\\":{\\\"value\\\":\\\"聚异丁烯（桶装）\\\"},\\\"cextsrcbid\\\":{},\\\"vextsrcbillcode\\\":{},\\\"vordercode\\\":{\\\"value\\\":\\\"CD2402280008\\\"},\\\"norigtaxprice\\\":{\\\"value\\\":\\\"17.5\\\"},\\\"vbdef45\\\":{},\\\"ngroupmny\\\":{\\\"value\\\":\\\"1533030.97\\\"},\\\"vbdef46\\\":{},\\\"pk_material\\\":{\\\"value\\\":\\\"1001A1100000000005XE\\\"},\\\"vbdef43\\\":{},\\\"casscustid\\\":{},\\\"nnumwrbck\\\":{},\\\"vbdef44\\\":{},\\\"csourcetypecode\\\":{\\\"value\\\":\\\"45\\\"},\\\"vbdef49\\\":{},\\\"vbdef47\\\":{},\\\"vbdef48\\\":{},\\\"vbdef41\\\":{},\\\"vbdef42\\\":{},\\\"vbdef40\\\":{},\\\"naccumsettmny\\\":{},\\\"ftaxtypeflag\\\":{\\\"value\\\":\\\"1\\\"},\\\"vfree10\\\":{},\\\"pk_order\\\":{\\\"value\\\":\\\"1001ZZ100000001TYMPS\\\"},\\\"pseudocolumn\\\":{\\\"value\\\":\\\"0\\\"},\\\"vfirstcode\\\":{\\\"value\\\":\\\"CD2402280008\\\"},\\\"castunitid\\\":{\\\"value\\\":\\\"1001A1100000000005GQ\\\"},\\\"pk_apfinanceorg_v\\\":{\\\"value\\\":\\\"0001A110000000002TJ9\\\"},\\\"cunitid\\\":{\\\"value\\\":\\\"1001A1100000000005GQ\\\"},\\\"cfirsttypecode\\\":{\\\"value\\\":\\\"21\\\"},\\\"ntaxmny\\\":{\\\"value\\\":\\\"1732325.00\\\"},\\\"pk_order_b\\\":{\\\"value\\\":\\\"1001ZZ100000001TYMPT\\\"},\\\"vordertrantype\\\":{\\\"value\\\":\\\"1001A110000000002KOR\\\"},\\\"vmemob\\\":{\\\"value\\\":\\\"\\\"},\\\"ntaxprice\\\":{\\\"value\\\":\\\"17.5000000000000\\\"},\\\"vbdef50\\\":{},\\\"cextsrcbilltype\\\":{},\\\"vsourcerowno\\\":{\\\"value\\\":\\\"10\\\"},\\\"firstts\\\":{},\\\"nastnum\\\":{\\\"value\\\":\\\"98990.0000000000000\\\"},\\\"ntax\\\":{\\\"value\\\":\\\"199294.03\\\"},\\\"vsourcecode\\\":{\\\"value\\\":\\\"RFYL2402280001\\\"},\\\"dbilldate\\\":{},\\\"cextsrcid\\\":{},\\\"csourceid\\\":{\\\"value\\\":\\\"1001ZZ100000001TYMR0\\\"},\\\"nnosubtaxrate\\\":{\\\"value\\\":\\\"0\\\"},\\\"cffileid\\\":{},\\\"ccontractrowid\\\":{},\\\"nglobalmny\\\":{\\\"value\\\":\\\"1533030.97\\\"},\\\"nnum\\\":{\\\"value\\\":\\\"98990.0000000000000\\\"},\\\"pk_invoice_b\\\":{},\\\"pk_material.materialspec\\\":{},\\\"vbdef9\\\":{},\\\"cfirstid\\\":{\\\"value\\\":\\\"1001ZZ100000001TYMPS\\\"},\\\"vctcode\\\":{},\\\"nglobaltaxmny\\\":{\\\"value\\\":\\\"1732325.00\\\"},\\\"ts\\\":{},\\\"pk_group\\\":{\\\"value\\\":\\\"0001A110000000000C8B\\\"},\\\"casscustvid\\\":{},\\\"frowtype\\\":{\\\"value\\\":\\\"0\\\"},\\\"pk_apliabcenter\\\":{},\\\"cadjustedrowid\\\":{},\\\"firstbts\\\":{},\\\"pk_usedept\\\":{},\\\"pk_stordoc\\\":{\\\"value\\\":\\\"1001A1100000000012LC\\\"},\\\"dr\\\":{},\\\"nsourcenum\\\":{\\\"value\\\":\\\"98990\\\"},\\\"nprice\\\":{\\\"value\\\":\\\"15.4900000000000\\\"},\\\"pk_org\\\":{\\\"value\\\":\\\"0001A110000000002TJA\\\"},\\\"vbdef8\\\":{},\\\"pk_supplier\\\":{},\\\"vbdef7\\\":{},\\\"vbdef6\\\":{},\\\"naccumsettnum\\\":{},\\\"vbdef5\\\":{},\\\"vbdef4\\\":{},\\\"vbdef3\\\":{},\\\"vbdef2\\\":{},\\\"vbdef1\\\":{},\\\"cprojecttaskid\\\":{},\\\"nreasonwastenum\\\":{},\\\"ngrouptaxmny\\\":{\\\"value\\\":\\\"1732325.00\\\"},\\\"vfree1\\\":{},\\\"sourcebts\\\":{\\\"value\\\":\\\"2024-02-28 15:04:24\\\"},\\\"vfirsttrantype\\\":{\\\"value\\\":\\\"1001A110000000002KOR\\\"},\\\"pk_apfinanceorg\\\":{\\\"value\\\":\\\"0001A110000000002TJA\\\"},\\\"pk_order_payplan\\\":{},\\\"vsourcetrantype\\\":{\\\"value\\\":\\\"1001A110000000002L2G\\\"},\\\"dsourcedate\\\":{\\\"value\\\":\\\"2024-02-28 15:04:18\\\"},\\\"vbdef12\\\":{},\\\"pk_supplier_v\\\":{},\\\"vbdef13\\\":{},\\\"vbdef10\\\":{},\\\"vbdef11\\\":{\\\"value\\\":\\\"24212000000008632727\\\"},\\\"nasttaxprice\\\":{\\\"value\\\":\\\"17.5000000000000\\\"},\\\"pk_srcmaterial\\\":{\\\"value\\\":\\\"1001A1100000000005XE\\\"},\\\"vbdef16\\\":{},\\\"vbdef17\\\":{},\\\"vbdef14\\\":{},\\\"ntaxrate\\\":{\\\"value\\\":\\\"13\\\"},\\\"vbdef15\\\":{},\\\"vfree8\\\":{},\\\"vfree9\\\":{},\\\"vfree6\\\":{},\\\"pk_org_v\\\":{\\\"value\\\":\\\"0001A110000000002TJ9\\\"},\\\"vfree7\\\":{},\\\"cproductorid\\\":{},\\\"vfree4\\\":{},\\\"nadjustorgprice\\\":{},\\\"vfree5\\\":{},\\\"vfree2\\\":{},\\\"vfirstrowno\\\":{\\\"value\\\":\\\"10\\\"},\\\"vfree3\\\":{},\\\"pk_order_payplan_b_ts\\\":{},\\\"pk_usedept_v\\\":{},\\\"cqualitylevelid\\\":{},\\\"norigtaxmny\\\":{\\\"value\\\":\\\"1732325.00\\\"},\\\"pk_invoice\\\":{},\\\"pk_stockps_b\\\":{},\\\"vbdef18\\\":{},\\\"vbdef19\\\":{},\\\"sourcets\\\":{\\\"value\\\":\\\"2024-02-28 15:04:26\\\"},\\\"vbdef23\\\":{},\\\"ctaxcodeid\\\":{\\\"value\\\":\\\"1002Z01000000001CNE2\\\"},\\\"vbdef24\\\":{},\\\"nastorigtaxprice\\\":{\\\"value\\\":\\\"17.5000000000000\\\"},\\\"vbdef21\\\":{},\\\"vbdef22\\\":{},\\\"csourcebid\\\":{\\\"value\\\":\\\"1001ZZ100000001TYMR1\\\"},\\\"vbdef27\\\":{},\\\"pk_apliabcenter_v\\\":{},\\\"vbdef28\\\":{},\\\"cadjustedinvid\\\":{},\\\"vbdef25\\\":{},\\\"vbdef26\\\":{},\\\"pk_costsubj\\\":{},\\\"pk_batchcode\\\":{},\\\"nplanprice\\\":{},\\\"vbdef20\\\":{},\\\"naccumnosubmny\\\":{},\\\"pk_order_payplan_ts\\\":{},\\\"cprojectid\\\":{},\\\"vchangerate\\\":{\\\"value\\\":\\\"1/1\\\"},\\\"pk_material.materialtype\\\":{},\\\"vbdef29\\\":{},\\\"norigprice\\\":{\\\"value\\\":\\\"15.486726\\\"},\\\"vbdef34\\\":{},\\\"vbdef35\\\":{},\\\"crowno\\\":{\\\"value\\\":\\\"10\\\"},\\\"vbdef32\\\":{},\\\"vbdef33\\\":{},\\\"ncaltaxmny\\\":{\\\"value\\\":\\\"1533030.97\\\"},\\\"pk_order_payplan_b\\\":{},\\\"vbdef38\\\":{},\\\"vbdef39\\\":{},\\\"ccontractid\\\":{},\\\"vbdef36\\\":{},\\\"nastorigprice\\\":{\\\"value\\\":\\\"15.4900000000000\\\"},\\\"vbdef37\\\":{},\\\"vbatchcode\\\":{},\\\"nmny\\\":{\\\"value\\\":\\\"1533030.97\\\"},\\\"norigmny\\\":{\\\"value\\\":\\\"1533030.97\\\"},\\\"vbdef30\\\":{},\\\"ncalcostmny\\\":{\\\"value\\\":\\\"1533030.97\\\"},\\\"vadjedbillcode\\\":{},\\\"vbdef31\\\":{},\\\"opr\\\":{},\\\"numberindex\\\":{\\\"value\\\":\\\"1\\\"}},\\\"key\\\":\\\"500520.0ba103219b576683\\\",\\\"rowId\\\":\\\"500520.0ba103219b576683\\\",\\\"rowid\\\":\\\"500520.0ba103219b576683\\\",\\\"pid\\\":\\\"root\\\",\\\"isLeaf\\\":true,\\\"depth\\\":0,\\\"path\\\":[\\\"500520.0ba103219b576683\\\"],\\\"height\\\":0,\\\"dataIndex\\\":0,\\\"leafIndex\\\":0,\\\"range\\\":[0,1],\\\"viewIndex\\\":0,\\\"selected\\\":false}],\\\"areacode\\\":\\\"card_body\\\"}},\\\"templetid\\\":\\\"1001A110000000002VWX\\\",\\\"skipCodes\\\":[]}\",\"code\":\"1000000000\",\"message\":null,\"errorStack\":null}";
        JSONObject jsonObject1 = JSONObject.fromObject(s1);
        if("true".equals(jsonObject1.optString("success"))){
            // 修复数据 根据单据号赋值给备注: TODO 备注=购买+供应商+物料+总数量
            String data = jsonObject1.optString("data");
            JSONObject jsonObject2 = JSONObject.fromObject(data);
            JSONObject jsonObject3 = jsonObject2.optJSONObject("head");
            JSONObject jsonObject4 = jsonObject3.optJSONObject("card_head");
            JSONArray jsonArray2 = jsonObject4.optJSONArray("rows");
            JSONObject data2 = new JSONObject();
            if(!jsonArray2.isEmpty()){
                JSONObject jsonObject5 = jsonArray2.optJSONObject(0);
                JSONObject jsonObject6 = jsonObject5.optJSONObject("values");
                JSONObject jsonObject7 = jsonObject6.optJSONObject("vdef12");
                String vbillcode = jsonObject7.optString("value");
                String paras = "购买";
                if(StringUtils.isNotEmpty(vbillcode)){
                    List<TranTaxHead> selectcgfpbycode = tranTaxMapper.selectcgfpbycode(vbillcode);
                    if(!selectcgfpbycode.isEmpty()){
                        TranTaxHead tranTaxHead = selectcgfpbycode.get(0);
                        String zydfpdid = tranTaxHead.getZydfpdid();
                        String gysmc = tranTaxHead.getGysmc();
//                    String fphm = tranTaxHead.getFphm();
                        String materiname = tranTaxHead.getMaterialname();
                        BigDecimal hsje = tranTaxHead.getHsje();
                        paras = paras+gysmc+materiname+hsje;
                        String materialdw = tranTaxHead.getMaterialdw();
                        if(StringUtils.isNotEmpty(materialdw)){
                            paras = paras + materialdw;
                        }
                        tranTaxHead.setBz(paras);
                        if(StringUtils.isNotEmpty(zydfpdid) && !"~".equals(zydfpdid)){
                            tranTaxMapper.updatexsfpbz(tranTaxHead);
                            tranTaxMapper.updateyfdglbz(tranTaxHead);
                        }
                        String fpdid = tranTaxHead.getFpdid();
                        String zydfpdid1 = tranTaxHead.getZydfpdid();
                        data2.put("fpdid",fpdid);
                        data2.put("zydfpdid",zydfpdid1);
                        //更新附件
                        JSONArray jsonArray3 = param.optJSONArray("fphmlb");
                        String zdr = param.optString("zdr");
                        JSONObject jsonObject8 = jsonArray3.getJSONObject(0);
                        String url = jsonObject8.optString("url");
                        if(StringUtils.isNotEmpty(url)){
                            TranTaxHead tth = new TranTaxHead();
                            tth.setFpdid(fpdid);
                            tth.setBz(url);
                            tth.setFphm(tranTaxHead.getFphm());
                            tth.setZdr(zdr);
                            savefileurl(tth);
                        }
                    }
                }
            }
            return new AjaxResult(0,"成功",data2);
        }else{
            return new AjaxResult(Integer.valueOf(jsonObject1.optString("code")),"失败",jsonObject1.optString("message"));
        }

    }

    /**
     * @author: Liujiaji
     * @date: 2024/4/3 15:59
     * @description: TODO 请求openapi保存采购发票
     * @param: [jsonArray1, param]
     * @return: void
     **/
    public void savecgfpapi(JSONArray jsonArray1,JSONObject param){
        //整理信息
        for (Object o : jsonArray1) {
            JSONObject jsonObject = JSONObject.fromObject(o);
            //获取采购入库单表头
            JSONObject jsonObject1 = jsonObject.optJSONObject("ic.PurchaseInHeadVO");
            JSONArray jsonArray2 = jsonObject.optJSONArray("ic.PurchaseInBodyVO");

            //组装请求参数
            //表头
            JSONObject po_invoice = clbt(jsonObject1, param);
            //表体
            JSONArray po_invoice_bj = clbti(jsonArray2,param);

            JSONObject po_invoice_hb = new JSONObject();
            po_invoice_hb.put("po_invoice",po_invoice);
            po_invoice_hb.put("po_invoice_b",po_invoice_bj);

            JSONArray po_invoice_hbj = new JSONArray();
            po_invoice_hbj.add(po_invoice_hb);

            JSONObject data = new JSONObject();
            data.put("data",po_invoice_hbj);
            String pram = data.toString();

            System.out.println("采购发票保存参数整理完成"+pram);

            String apiuri2 = "nccloud/api/pu/invoice/operation/save";
//        String apiuri2 = "nccloud/api/uapbd/customermanage/baseinfo/getCustomerbaseinfo";
            String s1 = OpenApiUtils.openApiSend(apiuri2, pram,"采购发票保存");
            System.out.println(s1);

        }

    }
    /**
     * @author: Liujiaji
     * @date: 2024/4/3 16:24
     * @description: TODO 处理表头信息
     * @param: [jsonObject1]
     * @return: net.sf.json.JSONObject
     **/
    public JSONObject clbtsc(JSONObject jsonObject1,JSONObject param){
        //获取模板
        String s = PurchaseParaUtils.purchaseAddParam();
        JSONObject jsonObject = JSONObject.fromObject(s);
        JSONObject jsonObject2 = jsonObject.optJSONObject("head");
        JSONObject jsonObject3 = jsonObject2.optJSONObject("card_head");
        JSONArray jsonArray = jsonObject3.optJSONArray("rows");
        for (Object o : jsonArray) {
            JSONObject jsonObject4 = JSONObject.fromObject(o);
            JSONObject jsonObject6 = jsonObject4.optJSONObject("values");
            JSONObject values = new JSONObject();
            //遍历key存值 与采购入库单做匹配

            Set set = jsonObject6.keySet();
            for (Object o1 : set) {
                JSONObject value = new JSONObject();
                String s2 = String.valueOf(o1);
                JSONObject jsonObject5 = jsonObject1.optJSONObject(s2);
                if(null == jsonObject5){
                    String pk_org_v = jsonObject1.optString(s2);
                    if(StringUtils.isNotEmpty(pk_org_v)){
                        value.put("value",pk_org_v);
                    }
                }else{
                    String pk = jsonObject2.optString("pk");
                    if(StringUtils.isNotEmpty(pk)){
                        value.put("value",pk);
                    }
                }
                values.put(s2,value);
            }
            //处理必传参数 采购入库单之外的参数
            JSONObject value = new JSONObject();
            value.put("value","cs");
            values.put("xxxpk",value);

            //封装剩余数据
            JSONObject row = new JSONObject();
            row.put("values",values);
            JSONArray rows = new JSONArray();
            rows.add(row);
            JSONObject card_head = new JSONObject();
            card_head.put("rows",rows);
            card_head.put("areaType","form");
            card_head.put("areacode","card_head");
            JSONObject head = new JSONObject();
            head.put("card_head",card_head);
            return head;
        }

        return null;
    }
    /**
     * @author: Liujiaji
     * @date: 2024/4/3 16:24
     * @description: TODO 处理表头信息
     * @param: [jsonObject1]
     * @return: net.sf.json.JSONObject
     **/
    public JSONObject clbt(JSONObject jsonObject1,JSONObject param){
        JSONObject po_invoice = new JSONObject();
        //获取模板
        String s = PurchaseParaUtils.savePurchaseParam();
        JSONObject jsonObject = JSONObject.fromObject(s);
        JSONObject jsonObject4 = jsonObject.optJSONObject("data");
        JSONObject jsonObject3 = jsonObject4.optJSONObject("po_invoice");
        //遍历key存值
        Set set = jsonObject3.keySet();
        for (Object o : set) {
            String s1 = String.valueOf(o);
            JSONObject jsonObject2 = jsonObject1.optJSONObject(s1);
            if(null == jsonObject2){
                String pk_org_v = jsonObject1.optString(s1);
                if(StringUtils.isNotEmpty(pk_org_v)){
                    po_invoice.put(s1,pk_org_v);
                }
            }else{
                String pk = jsonObject2.optString("pk");
                if(StringUtils.isNotEmpty(pk)){
                    po_invoice.put(s1,pk);
                }
            }
        }

        //增加非模板参数 处理非固定参数 处理税务平台传参
        //供应商和 付款单位
        JSONObject jsonObject2 = jsonObject1.optJSONObject("cvendorid");
        if(null == jsonObject2){
            String pk_org_v = jsonObject1.optString("cvendorid");
            if(StringUtils.isNotEmpty(pk_org_v)){
                po_invoice.put("pk_supplier",pk_org_v);
                po_invoice.put("pk_paytosupplier",pk_org_v);
            }
        }else{
            String pk = jsonObject2.optString("pk");
            if(StringUtils.isNotEmpty(pk)){
                po_invoice.put("pk_supplier",pk);
                po_invoice.put("pk_paytosupplier",pk);
            }
        }
        //供应商多版本和 付款单位多版本
        JSONObject jsonObject5 = jsonObject1.optJSONObject("cvendorvid");
        if(null == jsonObject5){
            String pk_org_v = jsonObject1.optString("cvendorvid");
            if(StringUtils.isNotEmpty(pk_org_v)){
                po_invoice.put("pk_supplier_v",pk_org_v);
                po_invoice.put("pk_paytosupplier_v",pk_org_v);
            }
        }else{
            String pk = jsonObject5.optString("pk");
            if(StringUtils.isNotEmpty(pk)){
                po_invoice.put("pk_supplier_v",pk);
                po_invoice.put("pk_paytosupplier_v",pk);
            }
        }
        //采购部门多版本
        JSONObject jsonObject6 = jsonObject1.optJSONObject("cdptvid");
        if(null == jsonObject6){
            String pk_org_v = jsonObject1.optString("cdptvid");
            if(StringUtils.isNotEmpty(pk_org_v)){
                po_invoice.put("pk_dept_v",pk_org_v);
            }
        }else{
            String pk = jsonObject6.optString("pk");
            if(StringUtils.isNotEmpty(pk)){
                po_invoice.put("pk_dept_v",pk);
            }
        }
        //采购部门
        JSONObject jsonObject8 = jsonObject1.optJSONObject("cdptid");
        if(null == jsonObject8){
            String pk_org_v = jsonObject1.optString("cdptvid");
            if(StringUtils.isNotEmpty(pk_org_v)){
                po_invoice.put("pk_dept",pk_org_v);
            }
        }else{
            String pk = jsonObject8.optString("pk");
            if(StringUtils.isNotEmpty(pk)){
                po_invoice.put("pk_dept",pk);
            }
        }

        //库存组织 多版本
        JSONObject jsonObject7 = jsonObject1.optJSONObject("pk_org_v");
        if(null == jsonObject7){
            String pk_org_v = jsonObject1.optString("pk_org_v");
            if(StringUtils.isNotEmpty(pk_org_v)){
                po_invoice.put("pk_stockorg_v",pk_org_v);
            }
        }else{
            String pk = jsonObject7.optString("pk");
            if(StringUtils.isNotEmpty(pk)){
                po_invoice.put("pk_stockorg_v",pk);
            }
        }
        //库存组织
        JSONObject jsonObject9 = jsonObject1.optJSONObject("pk_org");
        if(null == jsonObject9){
            String pk_org_v = jsonObject1.optString("pk_org");
            if(StringUtils.isNotEmpty(pk_org_v)){
                po_invoice.put("pk_stockorg",pk_org_v);
            }
        }else{
            String pk = jsonObject9.optString("pk");
            if(StringUtils.isNotEmpty(pk)){
                po_invoice.put("pk_stockorg",pk);
            }
        }
        //业务流程
        po_invoice.put("pk_busitype","0001A110000000000V1Q");
        //发票类型编码
        po_invoice.put("vtrantypecode","25-05");
        //是否三角贸易
        po_invoice.put("btriatradeflag","false");
        //发票分类
        po_invoice.put("finvoiceclass","0");
        //票到日期 当天
        po_invoice.put("darrivedate","2024-04-07 14:15:46");
        //币种
        po_invoice.put("corigcurrencyid","1002Z0100000000001K1");
        //本币币种
        po_invoice.put("ccurrencyid","1002Z0100000000001K1");
        //业务员
        po_invoice.put("pk_bizpsn","0001A11000000000386J");
        //是否期初发票
        po_invoice.put("binitial","false");
        //整单数量
        po_invoice.put("ntotalastnum","2");
        //整单价税合计
        po_invoice.put("ntotalorigmny","6.00");
        //虚拟发票标志
        po_invoice.put("bvirtual","false");
        //费用发票
        po_invoice.put("bfee","false");
        //制单人
        String zdrid = param.optString("zdrid");
        po_invoice.put("billmaker",zdrid);

        //金税发票号
        JSONArray jsonArray = param.optJSONArray("fphmlb");
        String ss = "";
        for (int i = 0; i < jsonArray.size(); i++) {
            if(i == (jsonArray.size()-1)){
                JSONObject jsonObject10 = jsonArray.getJSONObject(i);
                String fphm = jsonObject10.optString("fphm");
                ss=ss+fphm;
//                                            jsonArray.get(i)
//                                            ss=ss+jsonArray.get(i);
            }else{
                JSONObject jsonObject10 = jsonArray.getJSONObject(i);
                String fphm = jsonObject10.optString("fphm");
                ss=ss+fphm;
//                                            ss=ss+jsonArray.get(i)+",";
            }
        }
        po_invoice.put("vdef12",ss);
        return po_invoice;
    }

    /**
     * @author: Liujiaji
     * @date: 2024/4/3 16:24
     * @description: TODO 处理表体信息
     * @param: [jsonObject1]
     * @return: net.sf.json.JSONObject
     **/
    public JSONArray clbtisc(JSONArray jsonObject2,JSONObject param){
        //获取模板
        String s = PurchaseParaUtils.purchaseAddParam();
        JSONObject jsonObject3 = JSONObject.fromObject(s);
        JSONObject jsonObject5 = jsonObject3.optJSONObject("body");
        JSONObject jsonObject6 = jsonObject5.optJSONObject("card_body");
        JSONArray jsonArray1 = jsonObject6.optJSONArray("rows");
        for (Object o1 : jsonArray1) {
            JSONObject jsonObject1 = JSONObject.fromObject(o1);

        }
        //税务平台明细mxid与采购入库单cgeneralbid匹配 匹配之后生成

        JSONArray po_invoice_bj = new JSONArray();
        //遍历采购入库单表体
        for (Object o : jsonObject2) {
            JSONArray jsonArray = param.optJSONArray("item");
            for (Object o1 : jsonArray) {
                JSONObject jsonObject = JSONObject.fromObject(o1);
                String djbh = jsonObject.optString("djbh");
//                if(djbh.equals()){
//
//                }
            }
            JSONObject po_invoice_b = new JSONObject();
            JSONObject jsonObject = JSONObject.fromObject(o);


            JSONObject jsonObject4 = jsonObject5.optJSONObject("po_invoice_b");
            Set set = jsonObject4.keySet();
            for (Object o1 : set) {
                String s1 = String.valueOf(o1);
                JSONObject jsonObject1 = jsonObject.optJSONObject(s1);
                if(null == jsonObject1){
                    String nnosubtax = jsonObject.optString(s1);
                    if (StringUtils.isNotEmpty(nnosubtax)){
                        po_invoice_b.put(s1,nnosubtax);
                    }
                }else{
                    String pk = jsonObject1.optString("pk");
                    if (StringUtils.isNotEmpty(pk)){
                        po_invoice_b.put(s1,pk);
                    }
                }
            }
            //增加非模板参数 处理非固定参数 处理税务平台传参
            po_invoice_bj.add(po_invoice_b);
        }

        return po_invoice_bj;
    }
    /**
     * @author: Liujiaji
     * @date: 2024/4/3 16:24
     * @description: TODO 处理表体信息
     * @param: [jsonObject1]
     * @return: net.sf.json.JSONObject
     **/
    public JSONArray clbti(JSONArray jsonObject2,JSONObject param){

        JSONArray po_invoice_bj = new JSONArray();
        //遍历采购入库单表体
        for (Object o : jsonObject2) {
            JSONArray jsonArray = param.optJSONArray("item");
            for (Object o1 : jsonArray) {
                JSONObject jsonObject = JSONObject.fromObject(o1);
                String djbh = jsonObject.optString("djbh");
//                if(djbh.equals()){
//
//                }
            }
            JSONObject po_invoice_b = new JSONObject();
            JSONObject jsonObject = JSONObject.fromObject(o);
            //获取模板
            String s = PurchaseParaUtils.savePurchaseParam();
            JSONObject jsonObject3 = JSONObject.fromObject(s);
            JSONObject jsonObject5 = jsonObject3.optJSONObject("data");
            JSONObject jsonObject4 = jsonObject5.optJSONObject("po_invoice_b");
            Set set = jsonObject4.keySet();
            for (Object o1 : set) {
                String s1 = String.valueOf(o1);
                JSONObject jsonObject1 = jsonObject.optJSONObject(s1);
                if(null == jsonObject1){
                    String nnosubtax = jsonObject.optString(s1);
                    if (StringUtils.isNotEmpty(nnosubtax)){
                        po_invoice_b.put(s1,nnosubtax);
                    }
                }else{
                    String pk = jsonObject1.optString("pk");
                    if (StringUtils.isNotEmpty(pk)){
                        po_invoice_b.put(s1,pk);
                    }
                }
            }
            //增加非模板参数 处理非固定参数 处理税务平台传参
            po_invoice_bj.add(po_invoice_b);
        }

        return po_invoice_bj;
    }
    /**
     * @author: Liujiaji
     * @date: 2023/12/27 10:22
     * @description: TODO 根据采购入库单返回信息及采购发票模板生成采购发票入参
     * @param: [jsonArray1]
     * @return: java.lang.String
     **/
    public String matchparam(JSONArray jsonArray1,JSONObject param){
        int crowno = 10;
        String zdrid = param.optString("zdrid");
        String findidbycode = "";
        if(StringUtils.isNotEmpty(zdrid)){
            findidbycode = purTaxMapper.findidbycode(zdrid);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowdate = sdf.format(new Date());
        //获取采购发票固定模板参数
        String s = PurchaseParaUtils.purchaseAddParam();
        JSONObject jsonObject3 = JSONObject.fromObject(s);
        JSONObject jsonObject4 = jsonObject3.optJSONObject("head");
        JSONObject jsonObject5 = jsonObject4.optJSONObject("card_head");
        JSONArray jsonArray2 = jsonObject5.optJSONArray("rows");

        String svbillcode ="";
        String ctrantypeid ="";
        String ntotalnum ="";
        String dbilldate ="";
        String ts ="";

        for (int i2 = 0; i2 < jsonArray2.size(); i2++) {
            JSONObject jso = jsonArray2.getJSONObject(i2);
            JSONObject jsonObject1 = jso.optJSONObject("values");
            Set<String> set = jsonObject1.keySet();
            for (String s1 : set) {
                for (Object o : jsonArray1) {
                    JSONObject jsonObjectp1 = JSONObject.fromObject(o);
                    //整理表头信息
                    JSONObject jsonObjectp2 = jsonObjectp1.optJSONObject("ic.PurchaseInHeadVO");
                    svbillcode = jsonObjectp2.optString("vbillcode");
                    ctrantypeid = jsonObjectp2.optString("ctrantypeid");
                    ntotalnum = jsonObjectp2.optString("ntotalnum");
                    dbilldate = jsonObjectp2.optString("dbilldate");
                    ts = jsonObjectp2.optString("ts");

                    JSONObject jsonObject2 = jsonObject1.optJSONObject(s1);
                    if(null != jsonObject2){
                        if(StringUtils.isNotEmpty(jsonObject2.optString("value"))){
                            jsonObject2.put("value","");
                        }
                        //获取表头发票信息
                        if(s1.equals("vbillcode")){
                            //采购发票号
//                                String ss = "RF"+"6"+(int)(Math.random()*10000000);
//                                jsonObject2.put("value",ss);
                        }else if(s1.equals("vdef12")){
                            //金税发票号
                            JSONArray jsonArray = param.optJSONArray("fphmlb");
                            String ss = "";
                            for (int i = 0; i < jsonArray.size(); i++) {
                                if(i == (jsonArray.size()-1)){
                                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                                    String fphm = jsonObject.optString("fphm");
                                    ss=ss+fphm;
//                                            jsonArray.get(i)
//                                            ss=ss+jsonArray.get(i);
                                }else{
                                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                                    String fphm = jsonObject.optString("fphm");
                                    ss=ss+fphm;
//                                            ss=ss+jsonArray.get(i)+",";
                                }
                            }
                            jsonObject2.put("value",ss);
                        }else if(s1.equals("ctrantypeid")){
                            jsonObject2.put("value","0001A110000000002BLW");
                        }else if(s1.equals("finvoiceclass")){
                            jsonObject2.put("value","0");
                        }else if(s1.equals("dbilldate")){
                            jsonObject2.put("value",nowdate);
                        }else if(s1.equals("darrivedate")){
                            jsonObject2.put("value",nowdate);
                        }else if(s1.equals("pk_supplier")){
                            //供应商
                            jsonObject2.put("value",jsonObjectp2.optJSONObject("cvendorid").optString("pk"));
                        }else if(s1.equals("pk_supplier_v")){
                            //供应商
                            jsonObject2.put("value",jsonObjectp2.optJSONObject("cvendorid").optString("pk"));
                        } if(s1.equals("corigcurrencyid")){
                            //币种
                            String s2 = jsonArray1.getJSONObject(0).optJSONArray("ic.PurchaseInBodyVO").getJSONObject(0).optJSONObject("ccurrencyid").optString("pk");
                            jsonObject2.put("value",s2);
                        }else if(s1.equals("pk_bizpsn")){
                            //业务员
                            jsonObject2.put("value",jsonObjectp2.optJSONObject("cbizid").optString("pk"));
                        }else if(s1.equals("pk_dept_v")){
                            //采购部门
                            jsonObject2.put("value",jsonObjectp2.optJSONObject("cdptvid").optString("pk"));
                        }else if(s1.equals("pk_stockorg_v")){
                            //库存组织
                            jsonObject2.put("value",jsonObjectp2.optJSONObject("pk_org_v").optString("pk"));
                        }else if(s1.equals("pk_paytosupplier")){
                            //供应商
                            jsonObject2.put("value",jsonObjectp2.optJSONObject("cvendorid").optString("pk"));
                        }else if(s1.equals("binitial")){
                            jsonObject2.put("value",false);
                        }else if(s1.equals("vmemo")){
//                                jsonObject2.put("value",jsonObjectp2.optString("vnote"));
                        }else if(s1.equals("pk_dept")){
                            //部门
                            jsonObject2.put("value",jsonObjectp2.optJSONObject("cdptid").optString("pk"));
                        }else if(s1.equals("pk_stockorg")){
                            //组织
                            jsonObject2.put("value",jsonObjectp2.optJSONObject("pk_org").optString("pk"));
                        }else if(s1.equals("creator")){
                            //创建人
//                                jsonObject2.put("value",jsonObjectp2.optJSONObject("creator").optString("pk"));
                            jsonObject2.put("value",findidbycode);
                        }else if(s1.equals("billmaker")){
                            //制单人
//                                jsonObject2.put("value",jsonObjectp2.optJSONObject("creator").optString("pk"));8080 3306 4433 9187
                            jsonObject2.put("value",findidbycode);
                        }else if(s1.equals("vtrantypecode")){

                            jsonObject2.put("value","25-01");
                        }else if(s1.equals("creationtime")){

                            jsonObject2.put("value",nowdate);
                        }else if(s1.equals("ccurrencyid")){
                            //币种
                            String s2 = jsonArray1.getJSONObject(0).optJSONArray("ic.PurchaseInBodyVO").getJSONObject(0).optJSONObject("ccurrencyid").optString("pk");
                            jsonObject2.put("value",s2);
                        }else if(s1.equals("nexchangerate")){
                            //折本汇率
                            jsonObject2.put("value","1");
                        }else if(s1.equals("pk_busitype")){
                            //业务流程
                            jsonObject2.put("value",jsonObjectp2.optString("cbiztype"));
                        }else if(s1.equals("pk_purchaseorg")){
                            //组织
                            jsonObject2.put("value",jsonObjectp2.optJSONObject("pk_org").optString("pk"));
                        }else if(s1.equals("pk_purchaseorg_v")){

                            jsonObject2.put("value",jsonObjectp2.optJSONObject("pk_org_v").optString("pk"));
                        }else if(s1.equals("ftaxtypeflagh")){

                            jsonObject2.put("value","1");
                        }else if(s1.equals("ntaxrateh")){
                            //税率
                            String s2 = jsonArray1.getJSONObject(0).optJSONArray("ic.PurchaseInBodyVO").getJSONObject(0).optString("ntaxrate");
                            jsonObject2.put("value",s2);
                        }else if(s1.equals("ngroupexchgrate")){
                            //本位币汇率
                            jsonObject2.put("value","1");
                        }else if(s1.equals("nglobalexchgrate")){

                            jsonObject2.put("value","1");
                        }else if(s1.equals("def1")){

                            jsonObject2.put("value","");
                        }else if(s1.equals("fbillstatus")){

                            jsonObject2.put("value","0");
                        }else if(s1.equals("btriatradeflag")){

                            jsonObject2.put("value",false);
                        }else if(s1.equals("bopptaxflag")){

                            jsonObject2.put("value",false);
                        }else if(s1.equals("finvoicetype")){
                            //本位币汇率
                            jsonObject2.put("value","0");
                        }else if(s1.equals("pk_paytosupplier_v")){
                            //供应商
                            jsonObject2.put("value",jsonObjectp2.optJSONObject("cvendorvid").optString("pk"));
                        }


                        Iterator<String> keys = jsonObjectp2.keys();
                        while (keys.hasNext()) {
                            String key = keys.next();
                            if(key.equals(s1)){

                                if(s1.equals("vbillcode")){
                                    continue;
                                }else if(s1.equals("ctrantypeid")){
                                    jsonObject2.put("value","0001A110000000002BLW");
                                }else if(s1.equals("finvoiceclass")){
                                    jsonObject2.put("value","0");
                                }else if(s1.equals("dbilldate")){
                                    jsonObject2.put("value",nowdate);
                                }else if(s1.equals("darrivedate")){
                                    jsonObject2.put("value",nowdate);
                                }else if(s1.equals("pk_supplier")){
                                    //供应商
                                    jsonObject2.put("value",jsonObjectp2.optJSONObject("cvendorid").optString("pk"));
                                }else if(s1.equals("pk_supplier_v")){
                                    //供应商
                                    jsonObject2.put("value",jsonObjectp2.optJSONObject("cvendorid").optString("pk"));
                                }else if(s1.equals("corigcurrencyid")){
                                    //币种
                                    String s2 = jsonArray1.getJSONObject(0).optJSONArray("ic.PurchaseInBodyVO").getJSONObject(0).optJSONObject("ccurrencyid").optString("pk");
                                    jsonObject2.put("value",s2);
                                }else if(s1.equals("pk_bizpsn")){
                                    //业务员
                                    jsonObject2.put("value",jsonObjectp2.optJSONObject("cbizid").optString("pk"));
                                }else if(s1.equals("pk_dept_v")){
                                    //采购部门
                                    jsonObject2.put("value",jsonObjectp2.optJSONObject("cdptvid").optString("pk"));
                                }else if(s1.equals("pk_stockorg_v")){
                                    //库存组织
                                    jsonObject2.put("value",jsonObjectp2.optJSONObject("pk_org_v").optString("pk"));
                                }else if(s1.equals("pk_paytosupplier")){
                                    //供应商
                                    jsonObject2.put("value",jsonObjectp2.optJSONObject("cvendorid").optString("pk"));
                                }else if(s1.equals("binitial")){
                                    jsonObject2.put("value",false);
                                }else if(s1.equals("vmemo")){
                                    jsonObject2.put("value",jsonObjectp2.optString("vnote"));
                                }else if(s1.equals("pk_dept")){
                                    //部门
                                    jsonObject2.put("value",jsonObjectp2.optJSONObject("cdptid").optString("pk"));
                                }else if(s1.equals("pk_stockorg")){
                                    //组织
                                    jsonObject2.put("value",jsonObjectp2.optJSONObject("pk_org").optString("pk"));
                                }else if(s1.equals("creator")){
                                    //创建人
//                                    jsonObject2.put("value",jsonObjectp2.optJSONObject("creator").optString("pk"));
                                    jsonObject2.put("value",findidbycode);
                                }else if(s1.equals("vtrantypecode")){

                                    jsonObject2.put("value","25-01");
                                }else if(s1.equals("creationtime")){

                                    jsonObject2.put("value",nowdate);
                                }else if(s1.equals("ccurrencyid")){
                                    //币种
                                    String s2 = jsonArray1.getJSONObject(0).optJSONArray("ic.PurchaseInBodyVO").getJSONObject(0).optJSONObject("ccurrencyid").optString("pk");
                                    jsonObject2.put("value",s2);
                                }else if(s1.equals("nexchangerate")){
                                    //折本汇率
                                    jsonObject2.put("value","1");
                                }else if(s1.equals("pk_busitype")){
                                    //业务流程
                                    jsonObject2.put("value",jsonObjectp2.optString("cbiztype"));
                                }else if(s1.equals("pk_purchaseorg")){
                                    //组织
                                    jsonObject2.put("value",jsonObjectp2.optJSONObject("pk_org").optString("pk"));
                                }else if(s1.equals("pk_purchaseorg_v")){

                                    jsonObject2.put("value",jsonObjectp2.optJSONObject("pk_org_v").optString("pk"));
                                }else if(s1.equals("ftaxtypeflagh")){

                                    jsonObject2.put("value","1");
                                }else if(s1.equals("ntaxrateh")){
                                    //税率
                                    String s2 = jsonArray1.getJSONObject(0).optJSONArray("ic.PurchaseInBodyVO").getJSONObject(0).optString("ntaxrate");
                                    jsonObject2.put("value",s2);
                                }else if(s1.equals("ntaxrate")){
                                    //税率
                                    String s2 = jsonArray1.getJSONObject(0).optJSONArray("ic.PurchaseInBodyVO").getJSONObject(0).optString("ntaxrate");
                                    jsonObject2.put("value",s2);
                                }else if(s1.equals("ngroupexchgrate")){
                                    //本位币汇率
                                    jsonObject2.put("value","1");
                                }else if(s1.equals("nglobalexchgrate")){
                                    //本位币汇率
                                    jsonObject2.put("value","1");
                                }else if(s1.equals("def1")){
                                    //本位币汇率
                                    jsonObject2.put("value","");
                                }else if(s1.equals("fbillstatus")){
                                    //本位币汇率
                                    jsonObject2.put("value","0");
                                }else if(s1.equals("btriatradeflag")){
                                    //本位币汇率
                                    jsonObject2.put("value",false);
                                }else if(s1.equals("bopptaxflag")){
                                    //本位币汇率
                                    jsonObject2.put("value",false);
                                }else if(s1.equals("finvoicetype")){
                                    //本位币汇率
                                    jsonObject2.put("value","0");
                                }else if(s1.equals("pk_paytosupplier_v")){
                                    //供应商
                                    jsonObject2.put("value",jsonObjectp2.optJSONObject("cvendorvid").optString("pk"));
                                }else if(s1.equals("ts")){

                                }else if(s1.equals("dmakedate")){

                                }else if(s1.equals("approver")){
//                                    jsonObject2.put("value",jsonObjectp2.optJSONObject("creator").optString("pk"));
//                                    jsonObject2.put("value",findidbycode);
                                }else if(s1.equals("taudittime")){

                                }else if(s1.equals("vdef1")){

                                }else{
                                    if("ctrantypeid".equals(key)){
                                        System.out.println("lail");
                                    }
                                    JSONObject jsonObject6 = jsonObjectp2.optJSONObject(key);
                                    if(null != jsonObject6 ){
                                        if(StringUtils.isNotEmpty(jsonObject6.optString("pk"))){
                                            jsonObject2.put("value",jsonObject6.optString("pk"));
                                        }
                                    }else {
                                        if(StringUtils.isNotEmpty(jsonObjectp2.optString(key))){
                                            jsonObject2.put("value",jsonObjectp2.optString(key));
                                        }
                                    }
                                }

                            }
                        }
                    }
                }
            }
        }

        //处理表体
        JSONObject jsonObject6 = jsonObject3.optJSONObject("body");
        JSONObject jsonObject7 = jsonObject6.optJSONObject("card_body");
        JSONArray jsonArray3 = jsonObject7.optJSONArray("rows");

        //金税数据
        JSONArray jsonArray4 = param.optJSONArray("item");
        JSONObject jsonObjectp2 = jsonArray3.getJSONObject(0);//模板数据
        for (int i = 0; i < jsonArray4.size(); i++) { //遍历金税
            String pk = "";
            String mxid = "";
            JSONObject jsonObject1 = new JSONObject(); //复制模板数据
            Set set2 = jsonObjectp2.keySet();
            for (Object key : set2) {
                jsonObject1.put(key,jsonObjectp2.get(key));
            }
            JSONObject jsonObject = jsonArray4.getJSONObject(i);
            JSONObject jsonObject2n = jsonObject1.optJSONObject("values");


//            String uuid = UUID.randomUUID().toString();
            String uuid = "500520.0ba103219b576683";

            jsonObject1.put("key",uuid);
            jsonObject1.put("rowId",uuid);
            jsonObject1.put("rowid",uuid);
            JSONArray jsonArray5 = jsonObject1.optJSONArray("path");
            jsonArray5.clear();
            jsonArray5.add(0, uuid);


            //开始匹配数据
            for (Object o : jsonArray1) { //采购入库单数据
                JSONObject jsonObjectr1 = JSONObject.fromObject(o);
                JSONArray jsonObjectr2 = jsonObjectr1.optJSONArray("ic.PurchaseInBodyVO");
                for (Object o1 : jsonObjectr2) {//采购入库单数据
                    JSONObject jsonObject9 = JSONObject.fromObject(o1); //TODO erp采购入库单明细行
                    pk = jsonObject9.optString("cgeneralbid");
                    mxid = jsonObject.optString("mxid");
                    JSONArray jsonArray6 = jsonObject.optJSONArray("fpmx");
                    jsonObject.put("hsdj", new BigDecimal(jsonObject.optString("hsdj")).setScale(6, BigDecimal.ROUND_HALF_UP));
                    jsonObject.put("bhsdj", new BigDecimal(jsonObject.optString("bhsdj")).setScale(6, BigDecimal.ROUND_HALF_UP));
//                            String fphm="";
//                            for (int i1 = 0; i1 < jsonArray6.size(); i1++) {
//                                if(i1 == (jsonArray6.size()-1)){
//                                    fphm = fphm+jsonArray6.getJSONObject(i1).optString("fphm");
//                                }else{
//                                    fphm = fphm+jsonArray6.getJSONObject(i1).optString("fphm")+",";
//                                }
//                            }

                    //根据税务平台和采购入库单明细匹配
                    if (StringUtils.isNotEmpty(pk) && StringUtils.isNotEmpty(mxid) && pk.equals(mxid)) {
                        JSONObject jsonObject2 = new JSONObject();
                        Set<String> set2n = jsonObject2n.keySet();
                        for (String s1 : set2n) {
                            jsonObject2.put(s1,jsonObject2n.optString(s1));
                        }
                        Set<String> set = jsonObject2.keySet();
                        for (String s1 : set) { //遍历模板数据
                            JSONObject jsonObjectj8 = jsonObject2.optJSONObject(s1);
                            if (null != jsonObjectj8) {
                                JSONObject jsonObject8 = new JSONObject();
//                                if (StringUtils.isNotEmpty(jsonObject8.optString("value"))) {
//                                    jsonObject8.put("value", "");
//                                }
                                String s3 = jsonObject9.optJSONObject("ccurrencyid").optString("name");
                                if (!"人民币".equals(s3)) {
                                    return "";
                                }
                                if (s1.equals("nastnum")) {//数量
                                    jsonObject8.put("value", jsonObject.optString("spsl"));
                                } else if (s1.equals("nnum")) {//主数量
                                    jsonObject8.put("value", jsonObject.optString("spsl"));
                                } else if (s1.equals("vbdef11")) { //发票号
                                    jsonObject8.put("value", jsonObject.optString("fphm"));
                                } else if (s1.equals("nastorigtaxprice")) {//含税单价
                                    jsonObject8.put("value", jsonObject.optString("hsdj"));
                                } else if (s1.equals("nasttaxprice")) {//本币含税单价
                                    jsonObject8.put("value", jsonObject.optString("hsdj"));
                                } else if (s1.equals("ntaxprice")) {//本币含税单价
                                    jsonObject8.put("value", jsonObject.optString("hsdj"));
                                } else if (s1.equals("nastorigprice")) {//无税单价
                                    jsonObject8.put("value", jsonObject.optString("bhsdj"));
                                } else if (s1.equals("nastprice")) {//本币无税单价
                                    jsonObject8.put("value", jsonObject.optString("bhsdj"));
                                } else if (s1.equals("nprice")) {//本币无税单价
                                    jsonObject8.put("value", jsonObject.optString("bhsdj"));
                                } else if (s1.equals("norigmny")) {//无税金额
                                    jsonObject8.put("value", jsonObject.optString("bhsje"));
                                } else if (s1.equals("nmny")) {//本币无税金额
                                    jsonObject8.put("value", jsonObject.optString("bhsje"));
                                } else if (s1.equals("ncalcostmny")) {//计成本金额
                                    jsonObject8.put("value", jsonObject.optString("bhsje"));
                                } else if (s1.equals("ncaltaxmny")) {//计税金额
                                    jsonObject8.put("value", jsonObject.optString("bhsje"));
                                } else if (s1.equals("ntax")) {//税额
                                    jsonObject8.put("value", jsonObject.optString("se"));
                                } else if (s1.equals("norigtaxmny")) {//价税合计
                                    jsonObject8.put("value", jsonObject.optString("hsje"));
                                } else if (s1.equals("ntaxmny")) {//本币价税合计
                                    jsonObject8.put("value", jsonObject.optString("hsje"));
                                } else if (s1.equals("vsourcecode")) {//来源单据号
                                    jsonObject8.put("value", svbillcode);
                                } else if (s1.equals("cfirstbid")) {//源头单据行主键
                                    jsonObject8.put("value", jsonObject9.optString("cfirstbillbid"));
                                } else if (s1.equals("pk_material.name")) {//物料名称
                                    jsonObject8.put("value", jsonObject9.optJSONObject("cmaterialoid").optString("name"));
                                } else if (s1.equals("vordercode")) {//源头单据行主键
                                    jsonObject8.put("value", jsonObject9.optString("vfirstbillcode"));
                                } else if (s1.equals("pk_material")) {//物料主键
                                    jsonObject8.put("value", jsonObject9.optJSONObject("cmaterialoid").optString("pk"));
                                } else if (s1.equals("ctaxcodeid")) {//税码
                                    jsonObject8.put("value", jsonObject9.optJSONObject("ctaxcodeid").optString("pk"));
                                } else if (s1.equals("csourcetypecode")) {//来源单据类型
                                    jsonObject8.put("value", "45");
                                } else if (s1.equals("pk_order")) {//来源单据类型
                                    jsonObject8.put("value", jsonObject9.optString("cfirstbillhid"));
                                } else if (s1.equals("pseudocolumn")) {//来源单据类型
                                    jsonObject8.put("value", "0");
                                } else if (s1.equals("vfirstcode")) {
                                    jsonObject8.put("value", jsonObject9.optString("vfirstbillcode"));
                                } else if (s1.equals("pk_apfinanceorg_v")) {//组织
                                    jsonObject8.put("value", jsonObject9.optJSONObject("corpvid").optString("pk"));
                                } else if (s1.equals("cfirsttypecode")) {//组织
                                    jsonObject8.put("value", "21");
                                } else if (s1.equals("pk_order_b")) {
                                    jsonObject8.put("value", jsonObject9.optString("cfirstbillbid"));
                                } else if (s1.equals("vordertrantype")) {
                                    jsonObject8.put("value", jsonObject9.optString("cfirsttranstype"));
                                } else if (s1.equals("vmemob")) {
                                    jsonObject8.put("value", jsonObject9.optString("vnotebody"));
                                } else if (s1.equals("csourceid")) {
                                    jsonObject8.put("value", jsonObject9.optString("cgeneralhid"));
                                } else if (s1.equals("nglobalmny")) {//本币无税金额
                                    jsonObject8.put("value", jsonObject.optString("bhsje"));
                                } else if (s1.equals("cfirstid")) {//本币无税金额
                                    jsonObject8.put("value", jsonObject9.optString("cfirstbillhid"));
                                } else if (s1.equals("nglobaltaxmny")) {//价税合计
                                    jsonObject8.put("value", jsonObject.optString("hsje"));
                                } else if (s1.equals("ts")) {//价税合计

                                } else if (s1.equals("frowtype")) {//价税合计
                                    jsonObject8.put("value", "0");
                                } else if (s1.equals("pk_stordoc")) {//价税合计
                                    jsonObject8.put("value", jsonObject9.optJSONObject("cbodywarehouseid").optString("pk"));
                                } else if (s1.equals("nsourcenum")) {//价税合计
                                    jsonObject8.put("value", ntotalnum);
                                } else if (s1.equals("ngrouptaxmny")) {//价税合计
                                    jsonObject8.put("value", jsonObject.optString("hsje"));
                                } else if (s1.equals("sourcebts")) {//价税合计
                                    jsonObject8.put("value", jsonObject9.optString("ts"));
                                } else if (s1.equals("vfirsttrantype")) {//价税合计
                                    jsonObject8.put("value", jsonObject9.optString("cfirsttranstype"));
                                } else if (s1.equals("pk_apfinanceorg")) {//价税合计
                                    jsonObject8.put("value", jsonObject9.optJSONObject("cfanaceorgoid").optString("pk"));
                                } else if (s1.equals("vsourcetrantype")) {//价税合计
                                    jsonObject8.put("value", ctrantypeid);
                                } else if (s1.equals("pk_srcmaterial")) {//价税合计
                                    jsonObject8.put("value", jsonObject9.optJSONObject("csrcmaterialoid").optString("pk"));
                                } else if (s1.equals("csourcebid")) {//价税合计
                                    jsonObject8.put("value", jsonObject9.optString("cgeneralbid"));
                                } else if (s1.equals("numberindex")) {//价税合计
                                    jsonObject8.put("value", "1");
                                } else if (s1.equals("dsourcedate")) {//价税合计
                                    jsonObject8.put("value", dbilldate);
                                } else if (s1.equals("sourcets")) {//价税合计
                                    jsonObject8.put("value", ts);
                                }else if (s1.equals("crowno")) {//价税合计
                                    jsonObject8.put("value", String.valueOf(crowno));
                                    crowno = crowno+10;
                                }else if (s1.equals("cunitid")) {//主单位
                                    jsonObject8.put("value", jsonObject9.optJSONObject("cunitid").optString("pk"));
                                }else if (s1.equals("castunitid")) {//单位
                                    jsonObject8.put("value", jsonObject9.optJSONObject("castunitid").optString("pk"));
                                }else if (s1.equals("vchangerate")) {//换算率
                                    jsonObject8.put("value", jsonObject9.optString("vchangerate"));
                                }
                                jsonObject2.put(s1,jsonObject8);

                            }
                        }
                        jsonObject1.put("values",jsonObject2);
                        jsonArray3.add(jsonObject1);
                    }

                }
            }


        }
        jsonArray3.remove(0);
        return jsonObject3.toString();
    }

    /**
     * @author: Liujiaji
     * @date: 2023/12/27 10:22
     * @description: TODO 根据采购入库单返回信息及采购发票模板生成采购发票入参
     * @param: [jsonArray1]
     * @return: java.lang.String
     **/
    public String matchparam2(JSONArray jsonArray1,JSONObject param){
        for (Object o : jsonArray1) {
            JSONObject jsonObject = JSONObject.fromObject(o);
            //获取采购入库单表头
            JSONObject jsonObject1 = jsonObject.optJSONObject("ic.PurchaseInHeadVO");
            JSONArray jsonArray2 = jsonObject.optJSONArray("ic.PurchaseInBodyVO");

            //组装请求参数
            //表头
            JSONObject po_invoice = clbtsc(jsonObject1, param);
            //表体
            JSONArray po_invoice_bj = clbtisc(jsonArray2,param);

            JSONObject po_invoice_hb = new JSONObject();
            po_invoice_hb.put("pageid","400401600_card");
            po_invoice_hb.put("head",po_invoice);
            po_invoice_hb.put("body",po_invoice_bj);
            po_invoice_hb.put("templetid","1001A110000000002VWX");
            String pram = po_invoice_hb.toString();

            System.out.println("采购发票保存参数整理完成"+pram);




        }
        int crowno = 10;
        String zdrid = param.optString("zdrid");
        String findidbycode = "";
        if(StringUtils.isNotEmpty(zdrid)){
            findidbycode = purTaxMapper.findidbycode(zdrid);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowdate = sdf.format(new Date());
        //获取采购发票固定模板参数
        String s = PurchaseParaUtils.purchaseAddParam();
        JSONObject jsonObject3 = JSONObject.fromObject(s);
        JSONObject jsonObject4 = jsonObject3.optJSONObject("head");
        JSONObject jsonObject5 = jsonObject4.optJSONObject("card_head");
        JSONArray jsonArray2 = jsonObject5.optJSONArray("rows");

        String svbillcode ="";
        String ctrantypeid ="";
        String ntotalnum ="";
        String dbilldate ="";
        String ts ="";

        for (int i2 = 0; i2 < jsonArray2.size(); i2++) {
            JSONObject jso = jsonArray2.getJSONObject(i2);
            JSONObject jsonObject1 = jso.optJSONObject("values");
            Set<String> set = jsonObject1.keySet();
            for (String s1 : set) {
                    for (Object o : jsonArray1) {
                        JSONObject jsonObjectp1 = JSONObject.fromObject(o);
                        //整理表头信息
                        JSONObject jsonObjectp2 = jsonObjectp1.optJSONObject("ic.PurchaseInHeadVO");
                        svbillcode = jsonObjectp2.optString("vbillcode");
                        ctrantypeid = jsonObjectp2.optString("ctrantypeid");
                        ntotalnum = jsonObjectp2.optString("ntotalnum");
                        dbilldate = jsonObjectp2.optString("dbilldate");
                        ts = jsonObjectp2.optString("ts");

                        JSONObject jsonObject2 = jsonObject1.optJSONObject(s1);
                        if(null != jsonObject2){
                            if(StringUtils.isNotEmpty(jsonObject2.optString("value"))){
                                jsonObject2.put("value","");
                            }
                            //获取表头发票信息
                            if(s1.equals("vbillcode")){
                                //采购发票号
//                                String ss = "RF"+"6"+(int)(Math.random()*10000000);
//                                jsonObject2.put("value",ss);
                            }else if(s1.equals("vdef12")){
                                //金税发票号
                                JSONArray jsonArray = param.optJSONArray("fphmlb");
                                String ss = "";
                                    for (int i = 0; i < jsonArray.size(); i++) {
                                        if(i == (jsonArray.size()-1)){
                                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                                            String fphm = jsonObject.optString("fphm");
                                            ss=ss+fphm;
//                                            jsonArray.get(i)
//                                            ss=ss+jsonArray.get(i);
                                        }else{
                                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                                            String fphm = jsonObject.optString("fphm");
                                            ss=ss+fphm;
//                                            ss=ss+jsonArray.get(i)+",";
                                        }
                                    }
                                jsonObject2.put("value",ss);
                            }else if(s1.equals("ctrantypeid")){
                                jsonObject2.put("value","0001A110000000002BLW");
                            }else if(s1.equals("finvoiceclass")){
                                jsonObject2.put("value","0");
                            }else if(s1.equals("dbilldate")){
                                jsonObject2.put("value",nowdate);
                            }else if(s1.equals("darrivedate")){
                                jsonObject2.put("value",nowdate);
                            }else if(s1.equals("pk_supplier")){
                                //供应商
                                jsonObject2.put("value",jsonObjectp2.optJSONObject("cvendorid").optString("pk"));
                            }else if(s1.equals("pk_supplier_v")){
                                //供应商
                                jsonObject2.put("value",jsonObjectp2.optJSONObject("cvendorid").optString("pk"));
                            } if(s1.equals("corigcurrencyid")){
                                //币种
                                String s2 = jsonArray1.getJSONObject(0).optJSONArray("ic.PurchaseInBodyVO").getJSONObject(0).optJSONObject("ccurrencyid").optString("pk");
                                jsonObject2.put("value",s2);
                            }else if(s1.equals("pk_bizpsn")){
                                //业务员
                                jsonObject2.put("value",jsonObjectp2.optJSONObject("cbizid").optString("pk"));
                            }else if(s1.equals("pk_dept_v")){
                                //采购部门
                                jsonObject2.put("value",jsonObjectp2.optJSONObject("cdptvid").optString("pk"));
                            }else if(s1.equals("pk_stockorg_v")){
                                //库存组织
                                jsonObject2.put("value",jsonObjectp2.optJSONObject("pk_org_v").optString("pk"));
                            }else if(s1.equals("pk_paytosupplier")){
                                //供应商
                                jsonObject2.put("value",jsonObjectp2.optJSONObject("cvendorid").optString("pk"));
                            }else if(s1.equals("binitial")){
                                jsonObject2.put("value",false);
                            }else if(s1.equals("vmemo")){
//                                jsonObject2.put("value",jsonObjectp2.optString("vnote"));
                            }else if(s1.equals("pk_dept")){
                                //部门
                                jsonObject2.put("value",jsonObjectp2.optJSONObject("cdptid").optString("pk"));
                            }else if(s1.equals("pk_stockorg")){
                                //组织
                                jsonObject2.put("value",jsonObjectp2.optJSONObject("pk_org").optString("pk"));
                            }else if(s1.equals("creator")){
                                //创建人
//                                jsonObject2.put("value",jsonObjectp2.optJSONObject("creator").optString("pk"));
                                jsonObject2.put("value",findidbycode);
                            }else if(s1.equals("billmaker")){
                                //制单人
//                                jsonObject2.put("value",jsonObjectp2.optJSONObject("creator").optString("pk"));8080 3306 4433 9187
                                jsonObject2.put("value",findidbycode);
                            }else if(s1.equals("vtrantypecode")){

                                jsonObject2.put("value","25-01");
                            }else if(s1.equals("creationtime")){

                                jsonObject2.put("value",nowdate);
                            }else if(s1.equals("ccurrencyid")){
                                //币种
                                String s2 = jsonArray1.getJSONObject(0).optJSONArray("ic.PurchaseInBodyVO").getJSONObject(0).optJSONObject("ccurrencyid").optString("pk");
                                jsonObject2.put("value",s2);
                            }else if(s1.equals("nexchangerate")){
                                //折本汇率
                                jsonObject2.put("value","1");
                            }else if(s1.equals("pk_busitype")){
                                //业务流程
                                jsonObject2.put("value",jsonObjectp2.optString("cbiztype"));
                            }else if(s1.equals("pk_purchaseorg")){
                                //组织
                                jsonObject2.put("value",jsonObjectp2.optJSONObject("pk_org").optString("pk"));
                            }else if(s1.equals("pk_purchaseorg_v")){

                                jsonObject2.put("value",jsonObjectp2.optJSONObject("pk_org_v").optString("pk"));
                            }else if(s1.equals("ftaxtypeflagh")){

                                jsonObject2.put("value","1");
                            }else if(s1.equals("ntaxrateh")){
                                //税率
                                String s2 = jsonArray1.getJSONObject(0).optJSONArray("ic.PurchaseInBodyVO").getJSONObject(0).optString("ntaxrate");
                                jsonObject2.put("value",s2);
                            }else if(s1.equals("ntaxrate")){
                                //税率
                                String s2 = jsonArray1.getJSONObject(0).optJSONArray("ic.PurchaseInBodyVO").getJSONObject(0).optString("ntaxrate");
                                jsonObject2.put("value",s2);
                            }else if(s1.equals("ngroupexchgrate")){
                                //本位币汇率
                                jsonObject2.put("value","1");
                            }else if(s1.equals("nglobalexchgrate")){

                                jsonObject2.put("value","1");
                            }else if(s1.equals("def1")){

                                jsonObject2.put("value","");
                            }else if(s1.equals("fbillstatus")){

                                jsonObject2.put("value","0");
                            }else if(s1.equals("btriatradeflag")){

                                jsonObject2.put("value",false);
                            }else if(s1.equals("bopptaxflag")){

                                jsonObject2.put("value",false);
                            }else if(s1.equals("finvoicetype")){
                                //本位币汇率
                                jsonObject2.put("value","0");
                            }else if(s1.equals("pk_paytosupplier_v")){
                                //供应商
                                jsonObject2.put("value",jsonObjectp2.optJSONObject("cvendorvid").optString("pk"));
                            }


                            Iterator<String> keys = jsonObjectp2.keys();
                        while (keys.hasNext()) {
                            String key = keys.next();
                            if(key.equals(s1)){

                                if(s1.equals("vbillcode")){
                                    continue;
                                }else if(s1.equals("ctrantypeid")){
                                    jsonObject2.put("value","0001A110000000002BLW");
                                }else if(s1.equals("finvoiceclass")){
                                    jsonObject2.put("value","0");
                                }else if(s1.equals("dbilldate")){
                                    jsonObject2.put("value",nowdate);
                                }else if(s1.equals("darrivedate")){
                                    jsonObject2.put("value",nowdate);
                                }else if(s1.equals("pk_supplier")){
                                    //供应商
                                    jsonObject2.put("value",jsonObjectp2.optJSONObject("cvendorid").optString("pk"));
                                }else if(s1.equals("pk_supplier_v")){
                                    //供应商
                                    jsonObject2.put("value",jsonObjectp2.optJSONObject("cvendorid").optString("pk"));
                                }else if(s1.equals("corigcurrencyid")){
                                    //币种
                                    String s2 = jsonArray1.getJSONObject(0).optJSONArray("ic.PurchaseInBodyVO").getJSONObject(0).optJSONObject("ccurrencyid").optString("pk");
                                    jsonObject2.put("value",s2);
                                }else if(s1.equals("pk_bizpsn")){
                                    //业务员
                                    jsonObject2.put("value",jsonObjectp2.optJSONObject("cbizid").optString("pk"));
                                }else if(s1.equals("pk_dept_v")){
                                    //采购部门
                                    jsonObject2.put("value",jsonObjectp2.optJSONObject("cdptvid").optString("pk"));
                                }else if(s1.equals("pk_stockorg_v")){
                                    //库存组织
                                    jsonObject2.put("value",jsonObjectp2.optJSONObject("pk_org_v").optString("pk"));
                                }else if(s1.equals("pk_paytosupplier")){
                                    //供应商
                                    jsonObject2.put("value",jsonObjectp2.optJSONObject("cvendorid").optString("pk"));
                                }else if(s1.equals("binitial")){
                                    jsonObject2.put("value",false);
                                }else if(s1.equals("vmemo")){
                                    jsonObject2.put("value",jsonObjectp2.optString("vnote"));
                                }else if(s1.equals("pk_dept")){
                                    //部门
                                    jsonObject2.put("value",jsonObjectp2.optJSONObject("cdptid").optString("pk"));
                                }else if(s1.equals("pk_stockorg")){
                                    //组织
                                    jsonObject2.put("value",jsonObjectp2.optJSONObject("pk_org").optString("pk"));
                                }else if(s1.equals("creator")){
                                    //创建人
//                                  jsonObject2.put("value",jsonObjectp2.optJSONObject("creator").optString("pk"));
                                    jsonObject2.put("value",findidbycode);
                                }else if(s1.equals("vtrantypecode")){

                                    jsonObject2.put("value","25-01");
                                }else if(s1.equals("creationtime")){

                                    jsonObject2.put("value",nowdate);
                                }else if(s1.equals("ccurrencyid")){
                                    //币种
                                    String s2 = jsonArray1.getJSONObject(0).optJSONArray("ic.PurchaseInBodyVO").getJSONObject(0).optJSONObject("ccurrencyid").optString("pk");
                                    jsonObject2.put("value",s2);
                                }else if(s1.equals("nexchangerate")){
                                    //折本汇率
                                    jsonObject2.put("value","1");
                                }else if(s1.equals("pk_busitype")){
                                    //业务流程
                                    jsonObject2.put("value",jsonObjectp2.optString("cbiztype"));
                                }else if(s1.equals("pk_purchaseorg")){
                                    //组织
                                    jsonObject2.put("value",jsonObjectp2.optJSONObject("pk_org").optString("pk"));
                                }else if(s1.equals("pk_purchaseorg_v")){

                                    jsonObject2.put("value",jsonObjectp2.optJSONObject("pk_org_v").optString("pk"));
                                }else if(s1.equals("ftaxtypeflagh")){

                                    jsonObject2.put("value","1");
                                }else if(s1.equals("ntaxrateh")){
                                    //税率
                                    String s2 = jsonArray1.getJSONObject(0).optJSONArray("ic.PurchaseInBodyVO").getJSONObject(0).optString("ntaxrate");
                                    jsonObject2.put("value",s2);
                                }else if(s1.equals("ntaxrate")){
                                    //税率
                                    String s2 = jsonArray1.getJSONObject(0).optJSONArray("ic.PurchaseInBodyVO").getJSONObject(0).optString("ntaxrate");
                                    jsonObject2.put("value",s2);
                                }else if(s1.equals("ngroupexchgrate")){
                                    //本位币汇率
                                    jsonObject2.put("value","1");
                                }else if(s1.equals("nglobalexchgrate")){
                                    //本位币汇率
                                    jsonObject2.put("value","1");
                                }else if(s1.equals("def1")){
                                    //本位币汇率
                                    jsonObject2.put("value","");
                                }else if(s1.equals("fbillstatus")){
                                    //本位币汇率
                                    jsonObject2.put("value","0");
                                }else if(s1.equals("btriatradeflag")){
                                    //本位币汇率
                                    jsonObject2.put("value",false);
                                }else if(s1.equals("bopptaxflag")){
                                    //本位币汇率
                                    jsonObject2.put("value",false);
                                }else if(s1.equals("finvoicetype")){
                                    //本位币汇率
                                    jsonObject2.put("value","0");
                                }else if(s1.equals("pk_paytosupplier_v")){
                                    //供应商
                                    jsonObject2.put("value",jsonObjectp2.optJSONObject("cvendorvid").optString("pk"));
                                }else if(s1.equals("ts")){

                                }else if(s1.equals("dmakedate")){

                                }else if(s1.equals("approver")){
//                                    jsonObject2.put("value",jsonObjectp2.optJSONObject("creator").optString("pk"));
//                                    jsonObject2.put("value",findidbycode);
                                }else if(s1.equals("taudittime")){

                                }else if(s1.equals("vdef1")){

                                }else{
                                    if("ctrantypeid".equals(key)){
                                        System.out.println("lail");
                                    }
                                    JSONObject jsonObject6 = jsonObjectp2.optJSONObject(key);
                                    if(null != jsonObject6 ){
                                        if(StringUtils.isNotEmpty(jsonObject6.optString("pk"))){
                                            jsonObject2.put("value",jsonObject6.optString("pk"));
                                        }
                                    }else {
                                        if(StringUtils.isNotEmpty(jsonObjectp2.optString(key))){
                                            jsonObject2.put("value",jsonObjectp2.optString(key));
                                        }
                                    }
                                }

                            }
                        }
                    }
                    }
            }
        }

        //处理表体
        JSONObject jsonObject6 = jsonObject3.optJSONObject("body");
        JSONObject jsonObject7 = jsonObject6.optJSONObject("card_body");
        JSONArray jsonArray3 = jsonObject7.optJSONArray("rows");

        //金税数据
        JSONArray jsonArray4 = param.optJSONArray("item");
        JSONObject jsonObjectp2 = jsonArray3.getJSONObject(0);//模板数据
        for (int i = 0; i < jsonArray4.size(); i++) { //遍历金税
            String pk = "";
            String mxid = "";
            JSONObject jsonObject1 = new JSONObject(); //复制模板数据
            Set set2 = jsonObjectp2.keySet();
            for (Object key : set2) {
                jsonObject1.put(key,jsonObjectp2.get(key));
            }
            JSONObject jsonObject = jsonArray4.getJSONObject(i);
            JSONObject jsonObject2n = jsonObject1.optJSONObject("values");


//            String uuid = UUID.randomUUID().toString();
            String uuid = "500520.0ba103219b576683";

            jsonObject1.put("key",uuid);
            jsonObject1.put("rowId",uuid);
            jsonObject1.put("rowid",uuid);
            JSONArray jsonArray5 = jsonObject1.optJSONArray("path");
            jsonArray5.clear();
            jsonArray5.add(0, uuid);


            //开始匹配数据
            for (Object o : jsonArray1) { //采购入库单数据
                JSONObject jsonObjectr1 = JSONObject.fromObject(o);
                JSONArray jsonObjectr2 = jsonObjectr1.optJSONArray("ic.PurchaseInBodyVO");
                for (Object o1 : jsonObjectr2) {//采购入库单数据
                    JSONObject jsonObject9 = JSONObject.fromObject(o1); //TODO erp采购入库单明细行
                    pk = jsonObject9.optString("cgeneralbid");
                    mxid = jsonObject.optString("mxid");
                    JSONArray jsonArray6 = jsonObject.optJSONArray("fpmx");
                    jsonObject.put("hsdj", new BigDecimal(jsonObject.optString("hsdj")).setScale(6, BigDecimal.ROUND_HALF_UP));
                    jsonObject.put("bhsdj", new BigDecimal(jsonObject.optString("bhsdj")).setScale(6, BigDecimal.ROUND_HALF_UP));
//                            String fphm="";
//                            for (int i1 = 0; i1 < jsonArray6.size(); i1++) {
//                                if(i1 == (jsonArray6.size()-1)){
//                                    fphm = fphm+jsonArray6.getJSONObject(i1).optString("fphm");
//                                }else{
//                                    fphm = fphm+jsonArray6.getJSONObject(i1).optString("fphm")+",";
//                                }
//                            }

                    //根据税务平台和采购入库单明细匹配
                    if (StringUtils.isNotEmpty(pk) && StringUtils.isNotEmpty(mxid) && pk.equals(mxid)) {
                        JSONObject jsonObject2 = new JSONObject();
                        Set<String> set2n = jsonObject2n.keySet();
                        for (String s1 : set2n) {
                            jsonObject2.put(s1,jsonObject2n.optString(s1));
                        }
                        Set<String> set = jsonObject2.keySet();
                        for (String s1 : set) { //遍历模板数据
                            JSONObject jsonObjectj8 = jsonObject2.optJSONObject(s1);
                            if (null != jsonObjectj8) {
                                JSONObject jsonObject8 = new JSONObject();
//                                if (StringUtils.isNotEmpty(jsonObject8.optString("value"))) {
//                                    jsonObject8.put("value", "");
//                                }
                                String s3 = jsonObject9.optJSONObject("ccurrencyid").optString("name");
                                if (!"人民币".equals(s3)) {
                                    return "";
                                }
                                if (s1.equals("nastnum")) {//数量
                                    jsonObject8.put("value", jsonObject.optString("spsl"));
                                } else if (s1.equals("nnum")) {//主数量
                                    jsonObject8.put("value", jsonObject.optString("spsl"));
                                } else if (s1.equals("vbdef11")) { //发票号
                                    jsonObject8.put("value", jsonObject.optString("fphm"));
                                } else if (s1.equals("nastorigtaxprice")) {//含税单价
                                    jsonObject8.put("value", jsonObject.optString("hsdj"));
                                } else if (s1.equals("nasttaxprice")) {//本币含税单价
                                    jsonObject8.put("value", jsonObject.optString("hsdj"));
                                } else if (s1.equals("ntaxprice")) {//本币含税单价
                                    jsonObject8.put("value", jsonObject.optString("hsdj"));
                                } else if (s1.equals("nastorigprice")) {//无税单价
                                    jsonObject8.put("value", jsonObject.optString("bhsdj"));
                                } else if (s1.equals("nastprice")) {//本币无税单价
                                    jsonObject8.put("value", jsonObject.optString("bhsdj"));
                                } else if (s1.equals("nprice")) {//本币无税单价
                                    jsonObject8.put("value", jsonObject.optString("bhsdj"));
                                } else if (s1.equals("norigmny")) {//无税金额
                                    jsonObject8.put("value", jsonObject.optString("bhsje"));
                                } else if (s1.equals("nmny")) {//本币无税金额
                                    jsonObject8.put("value", jsonObject.optString("bhsje"));
                                } else if (s1.equals("ncalcostmny")) {//计成本金额
                                    jsonObject8.put("value", jsonObject.optString("bhsje"));
                                } else if (s1.equals("ncaltaxmny")) {//计税金额
                                    jsonObject8.put("value", jsonObject.optString("bhsje"));
                                } else if (s1.equals("ntax")) {//税额
                                    jsonObject8.put("value", jsonObject.optString("se"));
                                } else if (s1.equals("norigtaxmny")) {//价税合计
                                    jsonObject8.put("value", jsonObject.optString("hsje"));
                                } else if (s1.equals("ntaxmny")) {//本币价税合计
                                    jsonObject8.put("value", jsonObject.optString("hsje"));
                                } else if (s1.equals("vsourcecode")) {//来源单据号
                                    jsonObject8.put("value", svbillcode);
                                } else if (s1.equals("cfirstbid")) {//源头单据行主键
                                    jsonObject8.put("value", jsonObject9.optString("cfirstbillbid"));
                                } else if (s1.equals("pk_material.name")) {//物料名称
                                    jsonObject8.put("value", jsonObject9.optJSONObject("cmaterialoid").optString("name"));
                                } else if (s1.equals("vordercode")) {//源头单据行主键
                                    jsonObject8.put("value", jsonObject9.optString("vfirstbillcode"));
                                } else if (s1.equals("pk_material")) {//物料主键
                                    jsonObject8.put("value", jsonObject9.optJSONObject("cmaterialoid").optString("pk"));
                                } else if (s1.equals("csourcetypecode")) {//来源单据类型
                                    jsonObject8.put("value", "45");
                                } else if (s1.equals("pk_order")) {//来源单据类型
                                    jsonObject8.put("value", jsonObject9.optString("cfirstbillhid"));
                                } else if (s1.equals("pseudocolumn")) {//来源单据类型
                                    jsonObject8.put("value", "0");
                                } else if (s1.equals("vfirstcode")) {
                                    jsonObject8.put("value", jsonObject9.optString("vfirstbillcode"));
                                } else if (s1.equals("pk_apfinanceorg_v")) {//组织
                                    jsonObject8.put("value", jsonObject9.optJSONObject("corpvid").optString("pk"));
                                } else if (s1.equals("cfirsttypecode")) {//组织
                                    jsonObject8.put("value", "21");
                                } else if (s1.equals("pk_order_b")) {
                                    jsonObject8.put("value", jsonObject9.optString("cfirstbillbid"));
                                } else if (s1.equals("vordertrantype")) {
                                    jsonObject8.put("value", jsonObject9.optString("cfirsttranstype"));
                                } else if (s1.equals("vmemob")) {
                                    jsonObject8.put("value", jsonObject9.optString("vnotebody"));
                                } else if (s1.equals("csourceid")) {
                                    jsonObject8.put("value", jsonObject9.optString("cgeneralhid"));
                                } else if (s1.equals("nglobalmny")) {//本币无税金额
                                    jsonObject8.put("value", jsonObject.optString("bhsje"));
                                } else if (s1.equals("cfirstid")) {//本币无税金额
                                    jsonObject8.put("value", jsonObject9.optString("cfirstbillhid"));
                                } else if (s1.equals("nglobaltaxmny")) {//价税合计
                                    jsonObject8.put("value", jsonObject.optString("hsje"));
                                } else if (s1.equals("ts")) {//价税合计

                                } else if (s1.equals("frowtype")) {//价税合计
                                    jsonObject8.put("value", "0");
                                } else if (s1.equals("pk_stordoc")) {//价税合计
                                    jsonObject8.put("value", jsonObject9.optJSONObject("cbodywarehouseid").optString("pk"));
                                } else if (s1.equals("nsourcenum")) {//价税合计
                                    jsonObject8.put("value", ntotalnum);
                                } else if (s1.equals("ngrouptaxmny")) {//价税合计
                                    jsonObject8.put("value", jsonObject.optString("hsje"));
                                } else if (s1.equals("sourcebts")) {//价税合计
                                    jsonObject8.put("value", jsonObject9.optString("ts"));
                                } else if (s1.equals("vfirsttrantype")) {//价税合计
                                    jsonObject8.put("value", jsonObject9.optString("cfirsttranstype"));
                                } else if (s1.equals("pk_apfinanceorg")) {//价税合计
                                    jsonObject8.put("value", jsonObject9.optJSONObject("cfanaceorgoid").optString("pk"));
                                } else if (s1.equals("vsourcetrantype")) {//价税合计
                                    jsonObject8.put("value", ctrantypeid);
                                } else if (s1.equals("pk_srcmaterial")) {//价税合计
                                    jsonObject8.put("value", jsonObject9.optJSONObject("csrcmaterialoid").optString("pk"));
                                } else if (s1.equals("csourcebid")) {//价税合计
                                    jsonObject8.put("value", jsonObject9.optString("cgeneralbid"));
                                } else if (s1.equals("numberindex")) {//价税合计
                                    jsonObject8.put("value", "1");
                                } else if (s1.equals("dsourcedate")) {//价税合计
                                    jsonObject8.put("value", dbilldate);
                                } else if (s1.equals("sourcets")) {//价税合计
                                    jsonObject8.put("value", ts);
                                }else if (s1.equals("crowno")) {//价税合计
                                    jsonObject8.put("value", String.valueOf(crowno));
                                    crowno = crowno+10;
                                }else if (s1.equals("cunitid")) {//主单位
                                    jsonObject8.put("value", jsonObject9.optJSONObject("cunitid").optString("pk"));
                                }else if (s1.equals("castunitid")) {//单位
                                    jsonObject8.put("value", jsonObject9.optJSONObject("castunitid").optString("pk"));
                                }else if (s1.equals("vchangerate")) {//换算率
                                    jsonObject8.put("value", jsonObject9.optString("vchangerate"));
                                }
                                jsonObject2.put(s1,jsonObject8);

                            }
                        }
                        jsonObject1.put("values",jsonObject2);
                        jsonArray3.add(jsonObject1);
                    }

                }
            }


        }
        jsonArray3.remove(0);
        return jsonObject3.toString();
    }




    public AjaxResult findtest(){
        List<BdMaterial> all = bdMaterialMapper.findAll2();
        int i = 0;
        for (BdMaterial bdMaterial : all) {
            if(StringUtils.isNotEmpty(bdMaterial.getCode())){
                if(StringUtils.isNotEmpty(bdMaterial.getDef5()) || StringUtils.isNotEmpty(bdMaterial.getDef6())){
                    int updatebycode = bdMaterialMapper.updatebycode(bdMaterial);
                    i = i+updatebycode;
                }

                if("010101010000001".equals(bdMaterial.getCode())){

                }

            }
//            String def8 = bdMaterial.getDef8();
//            if(StringUtils.isNotEmpty(def8)){
//                if("A".equals(def8)){
//                    bdMaterial.setDef8("1001A9100000003FA19B");
//                }else if("B".equals(def8)){
//                    bdMaterial.setDef8("1001A9100000003FA19C");
//                }else if("C".equals(def8)){
//                    bdMaterial.setDef8("1001A9100000003FA19D");
//                }
//
//            }

        }
        System.out.println("已更新数据条数"+i);
        return new AjaxResult(200,"cg",all);
    }


    public AjaxResult findSwTax(JSONObject param, HttpServletRequest request){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        JSONObject searchValObject = param.optJSONObject("searchVal");

        JSONObject pj = new JSONObject();
        //设置查询条件

        //如果 查询条件为空 则 默认查询条件 三个月
        //开始时间
        Calendar instance = Calendar.getInstance();
        instance.set(Calendar.MONTH,-3);
        long timeInMillis = instance.getTimeInMillis();
        Date date = new Date(timeInMillis);
        String sdate = sdf.format(date);
        //结束时间
        String edate = sdf.format(new Date());
        //销方名称
        JSONObject jsonObject = param.optJSONObject("capcustvid");
        String xfname = jsonObject.optString("display");
        //hsdwdm
        JSONObject jsonObject1 = param.optJSONObject("pk_org");
        String hsdwdm = jsonObject1.optString("value");
        String orgcodebypk = "";
        if(StringUtils.isNotEmpty(hsdwdm)){
            orgcodebypk = purchaseMapper.orgcodebypk(hsdwdm);
        }

        // 销方名称和核算单位名称为空则返回空集
        if(StringUtils.isEmpty(xfname) || StringUtils.isEmpty(orgcodebypk)){
            return new AjaxResult(500,"失败");
        }
        //根据条件查询
        pj.put("kprqq",sdate);
        pj.put("kprqz",edate);
        pj.put("hsdwdm",orgcodebypk);
        pj.put("ppzt","0");
        if("海关".equals(xfname)){
            //发票类型代码17为海关,当为海关时销方名称应为空
            pj.put("fplxdm","17");
        }else{
            pj.put("xfmc",xfname);
        }

        //查询条件不为空则设置查询条件
        if(null != searchValObject){
            String vbillcode = param.optString("vbillcode");
            String sdbilldate = param.optString("sdbilldate");
            String edbilldate = param.optString("edbilldate");
            String fstatusflag = param.optString("fstatusflag");
            pj.remove("ppzt");
            if(StringUtils.isNotEmpty(vbillcode)){
                pj.put("fphm",vbillcode);
            }
            if(StringUtils.isNotEmpty(sdbilldate) && StringUtils.isNotEmpty(edbilldate)){
                try {
                    Date sparse = sdf.parse(sdbilldate);
                    Date eparse = sdf.parse(edbilldate);
                    pj.put("kprqq",sdf.format(sparse));
                    pj.put("kprqz",sdf.format(eparse));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            if(StringUtils.isNotEmpty(fstatusflag)){
                pj.put("ppzt",fstatusflag);
            }
        }
        String params = pj.toString();

//        String params = "{\"lrsjStart\":\"2023-10-23\",\"lrsjEnd\":\"2024-05-23\",\"xfmc\":\"新乡高新工业设备安装有限公司\",\"hsdwdm\":\"1001\",\"status\":\"0\"}\n";
        String url = sendUrl + "/api/ydcj/invoice/jxInvoiceQueryExt";
        logger.info("请求税务平台获取发票池发票信息接口 地址: " + url);
        logger.info("请求税务平台获取发票池发票信息接口 入参: " + params);
        String res = OkHttpUtils.okHttpPost(url, params);
//        res = fpcpush(url, params);
//        res = "{\"code\":\"0\",\"msg\":\"查询成功\",\"data\":[{\"insurance\":null,\"invoiceTypeName\":\"数电(专)\",\"fare\":null,\"amountBig\":null,\"sellerName\":\"新乡恒信建筑拆除工程有限公司\",\"pretaxAmount\":81163.37,\"sellerAddress\":\"\",\"buyerTaxId\":\"914107006149375190\",\"sellerBankInfo\":\"\",\"buyerAddress\":\"\",\"pdfUrl\":null,\"sellerTaxId\":\"91410700317651338G\",\"invoiceType\":\"1010901\",\"sl\":null,\"details\":[{\"taxRate\":\"0.01\",\"goodsname\":\"*劳务*CDF(原PDF-2)设备框架拆除项目\",\"goodsamount\":\"81163.37\",\"notaxprice\":\"81163.366336633700000\",\"measureunits\":\"项\",\"goodstaxamount\":\"811.63\"}],\"invoiceNo\":\"25412000000050970153\",\"cfd\":null,\"lrrmc\":\"自动采集\",\"caacDevelopmentFund\":null,\"otherTaxes\":null,\"invoiceDate\":\"2025-02-25\",\"buyerName\":\"新乡市瑞丰新材料股份有限公司\",\"mdd\":null,\"invoiceCode\":\"\",\"checkCode\":null,\"lrsj\":\"2025-02-25T19:15:44\",\"ifCheck\":1,\"buyerBankInfo\":\"\",\"invoiceId\":\"9788f784e5bf4f3f8ea8c1e69169247c\",\"sourceName\":null,\"taxAmount\":811.63,\"fuelSurcharge\":null,\"amountLittle\":81975.00}],\"total\":null}\n";

        logger.info("请求税务平台获取发票池发票信息接口 返参: " + res);
        //处理精度丢失问题
        Object parse1 = com.alibaba.fastjson.JSONObject.parse(res);
        JSONObject jsonObject2 = JSONObject.fromObject(parse1);
        List<FreightTax> reslist = new ArrayList<>();
        List<FreightTax> reslist2 = new ArrayList<>();
        if("0".equals(jsonObject2.optString("code"))){
            JSONArray jsonArray = jsonObject2.optJSONArray("data");
            for (Object o : jsonArray) {
                JSONObject jsonObject3 = JSONObject.fromObject(o);
                //发票红冲状态
                String fphczt = jsonObject3.optString("fpzt");
                //价税合计
                String amountLittle = jsonObject3.optString("hsje");
                //状态不是正常和部分红冲的要进行过滤
                if(!"0".equals(fphczt) && !"22".equals(fphczt)){
                    continue;
                }
                //发票号码
                String invoiceNo = jsonObject3.optString("fphm");
                //发票代码 , 将发票代码与发票号码合并代码-号码
                String fpdm = jsonObject3.optString("fpdm");
                if(StringUtils.isNotBlank(fpdm) && !"null".equals(fpdm)){
                    invoiceNo = fpdm + "-" + invoiceNo;
                }
                //开票日期
//                String invoiceDate = jsonObject3.optString("lrsj").replace("T"," ");
                String invoiceDate = jsonObject3.optString("kprq");
                Date parse = null;
                try {
                    parse = sdf.parse(invoiceDate);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                invoiceDate=sdf.format(parse);
//                String invoiceDate = jsonObject1.optString("lrsj");
                //销方名称
                String sellerName = jsonObject3.optString("xfmc");

                //无税金额
                String pretaxAmount = jsonObject3.optString("bhsje");
                //税额
                String taxAmount = jsonObject3.optString("se");
                //发票类型名称
                String fplxmc = jsonObject3.optString("fplxmc");
                //发票使用状态
                String status = jsonObject3.optString("ppzt");
                FreightTax ftvo = new FreightTax();
                ftvo.setVbillcode(invoiceNo);
                ftvo.setDbilldate(invoiceDate);
                if(StringUtils.isNotBlank(sellerName) && !"null".equals(sellerName)){
                    ftvo.setSeller_name(sellerName);
                }else{
                    ftvo.setSeller_name("");
                }
                //价税合计
                ftvo.setNtaxmny(amountLittle);
                //无税金额
                ftvo.setUntaxmny(pretaxAmount);
                //税额
                ftvo.setTaxmny(taxAmount);
                ftvo.setTax_type(fplxmc);
                //0-未使用  2-已使用
                status = "0".equals(status) ? "0" :"2";
                ftvo.setFstatusflag(status);
                BigDecimal bigDecimal = new BigDecimal(amountLittle);
                if(bigDecimal.compareTo(new BigDecimal(0))>0){
                    reslist.add(ftvo);
                }else{
                    reslist2.add(ftvo);
                }
            }
            reslist.addAll(reslist2);
        }
        //整理返回数据
        if(reslist.isEmpty()) return new AjaxResult(500,"失败");
        return new AjaxResult(200,"成功",reslist);
    }

    public String fpcpush(String url,String param){
        String res = "{\"code\":\"0\",\"msg\":null,\"data\":[{\"nsrsbh\":\"914107006149375190\",\"fpdm\":\"\",\"fphm\":\"25412000000107092316\",\"kprq\":\"2025-05-08\",\"fplxdm\":\"31\",\"fplxmc\":\"数电(专)\",\"gfmc\":\"新乡市瑞丰新材料股份有限公司\",\"gfsh\":\"914107006149375190\",\"xfmc\":\"河南自坤物流有限公司\",\"xfsh\":\"91410700MADN7UEHX7\",\"fpzt\":\"0\",\"bhsje\":\"11741.22\",\"hsje\":\"12797.93\",\"se\":\"1056.71\",\"ppzt\":\"0\",\"dlurl\":\"https://dppt.henan.chinatax.gov.cn:8443/v/2_25412000000107092316_2025050816551493HX718CF\",\"ofdurl\":\"https://dppt.henan.chinatax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=OFD&Jym=18CF&Fphm=25412000000107092316&Kprq=20250508165514&Czsj=1746705160905\",\"pdfurl\":\"https://dppt.henan.chinatax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=PDF&Jym=18CF&Fphm=25412000000107092316&Kprq=20250508165514&Czsj=1746705160905\",\"xmlurl\":\"https://dppt.henan.chinatax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=XML&Jym=18CF&Fphm=25412000000107092316&Kprq=20250508165514&Czsj=1746705160905\",\"localofdurl\":null,\"localpdfurl\":\"jxfp/2025-05-08/fbaccb81e7564d8e8cf5cd7617802610.pdf\",\"localxmlurl\":null},{\"nsrsbh\":\"914107006149375190\",\"fpdm\":\"\",\"fphm\":\"25412000000107067564\",\"kprq\":\"2025-05-08\",\"fplxdm\":\"31\",\"fplxmc\":\"数电(专)\",\"gfmc\":\"新乡市瑞丰新材料股份有限公司\",\"gfsh\":\"914107006149375190\",\"xfmc\":\"河南自坤物流有限公司\",\"xfsh\":\"91410700MADN7UEHX7\",\"fpzt\":\"0\",\"bhsje\":\"9306.42\",\"hsje\":\"10144.00\",\"se\":\"837.58\",\"ppzt\":\"0\",\"dlurl\":\"https://dppt.henan.chinatax.gov.cn:8443/v/2_25412000000107067564_2025050816583300HX79AB3\",\"ofdurl\":\"https://dppt.henan.chinatax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=OFD&Jym=9AB3&Fphm=25412000000107067564&Kprq=20250508165833&Czsj=1746705154588\",\"pdfurl\":\"https://dppt.henan.chinatax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=PDF&Jym=9AB3&Fphm=25412000000107067564&Kprq=20250508165833&Czsj=1746705154588\",\"xmlurl\":\"https://dppt.henan.chinatax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=XML&Jym=9AB3&Fphm=25412000000107067564&Kprq=20250508165833&Czsj=1746705154588\",\"localofdurl\":null,\"localpdfurl\":\"jxfp/2025-05-08/97c3ac8fd0724762b785a950b7fedcf3.pdf\",\"localxmlurl\":null},{\"nsrsbh\":\"914107006149375190\",\"fpdm\":\"\",\"fphm\":\"25412000000107056062\",\"kprq\":\"2025-05-08\",\"fplxdm\":\"31\",\"fplxmc\":\"数电(专)\",\"gfmc\":\"新乡市瑞丰新材料股份有限公司\",\"gfsh\":\"914107006149375190\",\"xfmc\":\"河南自坤物流有限公司\",\"xfsh\":\"91410700MADN7UEHX7\",\"fpzt\":\"0\",\"bhsje\":\"254279.67\",\"hsje\":\"277164.85\",\"se\":\"22885.18\",\"ppzt\":\"0\",\"dlurl\":\"https://dppt.henan.chinatax.gov.cn:8443/v/2_25412000000107056062_2025050816472485HX7B8A1\",\"ofdurl\":\"https://dppt.henan.chinatax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=OFD&Jym=B8A1&Fphm=25412000000107056062&Kprq=20250508164724&Czsj=1746705150138\",\"pdfurl\":\"https://dppt.henan.chinatax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=PDF&Jym=B8A1&Fphm=25412000000107056062&Kprq=20250508164724&Czsj=1746705150138\",\"xmlurl\":\"https://dppt.henan.chinatax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=XML&Jym=B8A1&Fphm=25412000000107056062&Kprq=20250508164724&Czsj=1746705150138\",\"localofdurl\":null,\"localpdfurl\":\"jxfp/2025-05-08/36fa472117d44b12a5c3e13343d9345a.pdf\",\"localxmlurl\":null},{\"nsrsbh\":\"914107006149375190\",\"fpdm\":\"\",\"fphm\":\"25412000000081923477\",\"kprq\":\"2025-04-04\",\"fplxdm\":\"81\",\"fplxmc\":\"数电(专)\",\"gfmc\":\"新乡市瑞丰新材料股份有限公司\",\"gfsh\":\"914107006149375190\",\"xfmc\":\"河南自坤物流有限公司\",\"xfsh\":\"91410700MADN7UEHX7\",\"fpzt\":\"0\",\"bhsje\":\"17273.39\",\"hsje\":\"18828.00\",\"se\":\"1554.61\",\"ppzt\":\"0\",\"dlurl\":\"https://dppt.henan.chinatax.gov.cn:8443/v/2_25412000000081923477_2025040411330500HX7D259\",\"ofdurl\":\"https://dppt.henan.chinatax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=OFD&Jym=D259&Fphm=25412000000081923477&Kprq=20250404113305&Czsj=1743745830682\",\"pdfurl\":\"https://dppt.henan.chinatax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=PDF&Jym=D259&Fphm=25412000000081923477&Kprq=20250404113305&Czsj=1743745830682\",\"xmlurl\":\"https://dppt.henan.chinatax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=XML&Jym=D259&Fphm=25412000000081923477&Kprq=20250404113305&Czsj=1743745830682\",\"localofdurl\":null,\"localpdfurl\":\"jxfp/2025-04-04/e4391be25d9d4a7eab52b3ef3dd63bf0.pdf\",\"localxmlurl\":null},{\"nsrsbh\":\"914107006149375190\",\"fpdm\":\"\",\"fphm\":\"25412000000081895399\",\"kprq\":\"2025-04-04\",\"fplxdm\":\"81\",\"fplxmc\":\"数电(专)\",\"gfmc\":\"新乡市瑞丰新材料股份有限公司\",\"gfsh\":\"914107006149375190\",\"xfmc\":\"河南自坤物流有限公司\",\"xfsh\":\"91410700MADN7UEHX7\",\"fpzt\":\"0\",\"bhsje\":\"52772.84\",\"hsje\":\"57522.40\",\"se\":\"4749.56\",\"ppzt\":\"0\",\"dlurl\":\"https://dppt.henan.chinatax.gov.cn:8443/v/2_25412000000081895399_2025040411211240HX76747\",\"ofdurl\":\"https://dppt.henan.chinatax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=OFD&Jym=6747&Fphm=25412000000081895399&Kprq=20250404112112&Czsj=1743745826233\",\"pdfurl\":\"https://dppt.henan.chinatax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=PDF&Jym=6747&Fphm=25412000000081895399&Kprq=20250404112112&Czsj=1743745826233\",\"xmlurl\":\"https://dppt.henan.chinatax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=XML&Jym=6747&Fphm=25412000000081895399&Kprq=20250404112112&Czsj=1743745826233\",\"localofdurl\":null,\"localpdfurl\":\"jxfp/2025-04-04/8ea57c07482d4af1ae9db9c230243367.pdf\",\"localxmlurl\":null},{\"nsrsbh\":\"914107006149375190\",\"fpdm\":\"\",\"fphm\":\"25412000000081885480\",\"kprq\":\"2025-04-04\",\"fplxdm\":\"81\",\"fplxmc\":\"数电(专)\",\"gfmc\":\"新乡市瑞丰新材料股份有限公司\",\"gfsh\":\"914107006149375190\",\"xfmc\":\"河南自坤物流有限公司\",\"xfsh\":\"91410700MADN7UEHX7\",\"fpzt\":\"0\",\"bhsje\":\"138793.89\",\"hsje\":\"151285.35\",\"se\":\"12491.46\",\"ppzt\":\"0\",\"dlurl\":\"https://dppt.henan.chinatax.gov.cn:8443/v/2_25412000000081885480_2025040411130535HX75459\",\"ofdurl\":\"https://dppt.henan.chinatax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=OFD&Jym=5459&Fphm=25412000000081885480&Kprq=20250404111305&Czsj=1743745817347\",\"pdfurl\":\"https://dppt.henan.chinatax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=PDF&Jym=5459&Fphm=25412000000081885480&Kprq=20250404111305&Czsj=1743745817347\",\"xmlurl\":\"https://dppt.henan.chinatax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=XML&Jym=5459&Fphm=25412000000081885480&Kprq=20250404111305&Czsj=1743745817347\",\"localofdurl\":null,\"localpdfurl\":\"jxfp/2025-04-04/384e507a7a654c3eae8917b65822e51b.pdf\",\"localxmlurl\":null},{\"nsrsbh\":\"914107006149375190\",\"fpdm\":\"\",\"fphm\":\"24412000000219786517\",\"kprq\":\"2024-12-04\",\"fplxdm\":\"31\",\"fplxmc\":\"数电(专)\",\"gfmc\":\"新乡市瑞丰新材料股份有限公司\",\"gfsh\":\"914107006149375190\",\"xfmc\":\"河南自坤物流有限公司\",\"xfsh\":\"91410700MADN7UEHX7\",\"fpzt\":\"0\",\"bhsje\":\"513.76\",\"hsje\":\"560.00\",\"se\":\"46.24\",\"ppzt\":\"0\",\"dlurl\":\"https://dppt.henan.chinatax.gov.cn:8443/v/2_24412000000219786517_2024120417082400HX7F1E3\",\"ofdurl\":\"https://dppt.henan.chinatax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=OFD&Jym=F1E3&Fphm=24412000000219786517&Kprq=20241204170824&Czsj=1733313176961\",\"pdfurl\":\"https://dppt.henan.chinatax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=PDF&Jym=F1E3&Fphm=24412000000219786517&Kprq=20241204170824&Czsj=1733313176961\",\"xmlurl\":\"https://dppt.henan.chinatax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=XML&Jym=F1E3&Fphm=24412000000219786517&Kprq=20241204170824&Czsj=1733313176961\",\"localofdurl\":null,\"localpdfurl\":\"jxfp/2024-12-04/5cf1fd63cb4247df8a0beb003a33393d.pdf\",\"localxmlurl\":null},{\"nsrsbh\":\"914107006149375190\",\"fpdm\":\"\",\"fphm\":\"24412000000219726875\",\"kprq\":\"2024-12-04\",\"fplxdm\":\"31\",\"fplxmc\":\"数电(专)\",\"gfmc\":\"新乡市瑞丰新材料股份有限公司\",\"gfsh\":\"914107006149375190\",\"xfmc\":\"河南自坤物流有限公司\",\"xfsh\":\"91410700MADN7UEHX7\",\"fpzt\":\"0\",\"bhsje\":\"239891.15\",\"hsje\":\"261481.34\",\"se\":\"21590.19\",\"ppzt\":\"0\",\"dlurl\":\"https://dppt.henan.chinatax.gov.cn:8443/v/2_24412000000219726875_2024120417043334HX7C4CE\",\"ofdurl\":\"https://dppt.henan.chinatax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=OFD&Jym=C4CE&Fphm=24412000000219726875&Kprq=20241204170433&Czsj=1733313163205\",\"pdfurl\":\"https://dppt.henan.chinatax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=PDF&Jym=C4CE&Fphm=24412000000219726875&Kprq=20241204170433&Czsj=1733313163205\",\"xmlurl\":\"https://dppt.henan.chinatax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=XML&Jym=C4CE&Fphm=24412000000219726875&Kprq=20241204170433&Czsj=1733313163205\",\"localofdurl\":null,\"localpdfurl\":\"jxfp/2024-12-04/82533e64a6c449e99981fc6ae107fcaa.pdf\",\"localxmlurl\":null},{\"nsrsbh\":\"914107006149375190\",\"fpdm\":\"\",\"fphm\":\"24412000000195207805\",\"kprq\":\"2024-11-04\",\"fplxdm\":\"31\",\"fplxmc\":\"数电(专)\",\"gfmc\":\"新乡市瑞丰新材料股份有限公司\",\"gfsh\":\"914107006149375190\",\"xfmc\":\"河南自坤物流有限公司\",\"xfsh\":\"91410700MADN7UEHX7\",\"fpzt\":\"0\",\"bhsje\":\"268816.20\",\"hsje\":\"293009.67\",\"se\":\"24193.47\",\"ppzt\":\"0\",\"dlurl\":\"https://dppt.henan.chinatax.gov.cn:8443/v/2_24412000000195207805_2024110419331167HX75563\",\"ofdurl\":\"https://dppt.henan.chinatax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=OFD&Jym=5563&Fphm=24412000000195207805&Kprq=20241104193311&Czsj=1730724605603\",\"pdfurl\":\"https://dppt.henan.chinatax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=PDF&Jym=5563&Fphm=24412000000195207805&Kprq=20241104193311&Czsj=1730724605603\",\"xmlurl\":\"https://dppt.henan.chinatax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=XML&Jym=5563&Fphm=24412000000195207805&Kprq=20241104193311&Czsj=1730724605603\",\"localofdurl\":null,\"localpdfurl\":\"jxfp/2024-11-04/61bf6d714dab475faf73842755166606.pdf\",\"localxmlurl\":null}],\"total\":9}\n";
        return res;
    }

    /**
     * @author: Liujiaji
     * @date: 2024/3/27 15:56
     * @description: TODO 应付运费发票-应付单管理推送发票到税务平台
     * @param: [param, request]
     * @return: com.ruoyi.common.core.domain.AjaxResult
     **/
    public AjaxResult pushSwTax(JSONObject param, HttpServletRequest request){
        logger.info("应付运费发票-应付单管理推送发票到税务平台参数: "+param.toString());
        //判断单据类型
        String billType = param.optString("billType");
        if("F1".equals(billType)){
            //应付单推送税务平台
            pushSwTaxyfd(param);
        }else if("puinvoice".equals(billType)){
            //采购发票推送税务平台
            pushSwTaxcgfp(param);
        }else if("apinvoice".equals(billType)){
            //应付运费发票
            pushSwTaxyfyf(param);
        }
        return new AjaxResult(200,"成功");
    }

    /**
     * @author: LiuJiaji
     * @date: 2025/4/14 09:14
     * @description: TODO 费控报销发票推送税务平台
     * @param:
     * @return:
     **/
    public AjaxResult findCostToTax(String sdate,String edate){

        String uuid = IdGen.uuid();
        String sendfkurl = "/fcopen/fcoutapi";
        String discribe = "请求费控查询报销发票信息";
        String service = "FCINV_OUTAPI_queryInvoiceList";
        JSONObject param = new JSONObject();
        param.put("qybh","RICHFUL");
        param.put("dateType","1");
        param.put("startDate",sdate);
        param.put("endDate",edate);
        param.put("reimbursementStatusCode","1");//报销状态编码 0未报销 1已报销
        param.put("count",100);
        int i = 0;
        for (int j = 0; j <= i ; j++) {
            param.put("start",j);
//            param.put("start",3);

        JSONObject pushtofk = costHandleService.pushtofk(uuid, sendfkurl, service, discribe, param);
        String status = pushtofk.optString("status");
        //处理结果
        if("200".equals(status)){
            JSONObject jsonObject = pushtofk.optJSONObject("result");
            String total = jsonObject.optString("total");
            if(StringUtils.isBlank(total)){
                break;
            }
            //向上取
            BigDecimal divide = new BigDecimal(total).divide(new BigDecimal(100), 0, BigDecimal.ROUND_UP);
            i=divide.intValue();

            JSONArray fpjh = jsonObject.optJSONArray("fpjh");
            if(null !=fpjh && fpjh.size()>0){
                for (Object o : fpjh) {
                    JSONObject jsonObject1 = (JSONObject)o;
                    JSONObject data = new JSONObject();
                    //发票单id
                    data.put("fpdid",jsonObject1.optString("fpid"));
                    //单据号业务员可见对应报销单号
                    data.put("zydfpdid",jsonObject1.optString("bxdh"));
                    //制单日期
//                    data.put("zdrq",jsonObject1.optString(jsonObject1.optString("vouAccountDate").substring(0,10)));
                    String s1 = jsonObject1.optString("kprq");
                    if(StringUtils.isNotBlank(s1)){
                        data.put("autoCheckPeriod",s1.replace("-","").substring(0,6));
                    }

                    //供应商名称
                    data.put("gysmc",jsonObject1.optString("xfmc"));
                    //备注
                    data.put("bz",jsonObject1.optString("bzbz"));
                    //制单人
//                data.put("zdr",jsonObject1.optString("bzbz"));
                    //单据含税金额
                    data.put("hsje",jsonObject1.optString("jshj"));
                    //核算单位名称
                    data.put("hsdmmc",jsonObject1.optString("spfmc"));
                    //核算单位代码
                    data.put("hsdmdm",jsonObject1.optString("frgsdm"));
                    //所属税期
                    if(StringUtils.isNotBlank(jsonObject1.optString("approvalFinishTime"))){
                        data.put("autoCheckPeriod",jsonObject1.optString("approvalFinishTime").replace("-","").substring(0,6));
                    }
                    //发票类型
                    String fplx = jsonObject1.optString("fplx");
                    //发票代码
                    if(StringUtils.isNotBlank(jsonObject1.optString("fpdm"))){
                        if(StringUtils.isNotBlank(fplx) && "railway_e_ticket".equals(fplx)){
                            data.put("fphmlb",jsonObject1.optString("fphm"));
                        }else{
                            data.put("fphmlb",jsonObject1.optString("fpdm")+"-"+jsonObject1.optString("fphm"));
                        }
                    }else{
                        data.put("fphmlb",jsonObject1.optString("fphm"));
                    }
                    if(StringUtils.isNotBlank(fplx) && "quota_invoice".equals(fplx)){
                        continue;
                    }
                    JSONObject jdata = new JSONObject();
                    JSONArray jsonArray = new JSONArray();
                    jsonArray.add(jdata);
                    data.put("item",jsonArray);

                    JSONObject jsonObject2 = new JSONObject();
                    jsonObject2.put("code","0");
                    jsonObject2.put("msg","成功");
                    jsonObject2.put("data",data);
                    String params = jsonObject2.toString();
                    String url = sendUrl + "/jx/abutment/receiveBillInvoice";
                    //推送到税务平台
                    discribe = "费控报销单发票信息推送金税平台";
                    logger.info(uuid+discribe+"url"+url);
                    logger.info(uuid+discribe+"入参"+params);
                    String s = OkHttpUtils.okHttpPost(url, params);
                    logger.info(uuid+discribe+"返回参数"+s);
                }
            }
        }
        }
        return new AjaxResult(200,"成功");
    }

    /**
     * @author: Liujiaji
     * @date: 2024/3/28 09:08
     * @description: TODO 采购发票推送到税务平台
     * @param: [param]
     * @return: com.ruoyi.common.core.domain.AjaxResult
     **/
    public AjaxResult pushSwTaxcgfp(JSONObject param){
        JSONArray jsonArray = param.optJSONArray("dataInfo");
        String cinvoice_hid = param.optString("cinvoice_hid");
        if (null != jsonArray && jsonArray.size()>0){
            //单个和批量提交
            for (Object o : jsonArray) {
                JSONObject jsonObject = JSONObject.fromObject(o);
                String id = jsonObject.optString("id");
                findcgfpbyid(id);
            }
        }else if(StringUtils.isNotEmpty(cinvoice_hid)){
            //保存提交
            findcgfpbyid(cinvoice_hid);
        }
        return new AjaxResult(200,"成功");
    }

    public void pushtojspt (List<TranTaxHead> tranTaxHeads, String desc){
        for (TranTaxHead tranTaxHead : tranTaxHeads) {
            String fphmlb = tranTaxHead.getFphmlb();
            if(StringUtils.isNotEmpty(fphmlb)){
                JSONObject jsonObject1 = JSONObject.fromObject(tranTaxHead);
                JSONObject item = new JSONObject();
                item.put("djbh",jsonObject1.optString("djbh"));
                item.put("zdydjbh",jsonObject1.optString("zdydjbh"));
                item.put("fphmlb",jsonObject1.optString("fphm"));
                JSONArray jsa = new JSONArray();
                jsa.add(item);
                jsonObject1.put("item",jsa);
                JSONObject pjs = new JSONObject();
                pjs.put("data",jsonObject1);
                pjs.put("code",0);
                pjs.put("msg","成功");

                String params = pjs.toString();
                String url = sendUrl + "/jx/abutment/receiveBillInvoice";
                logger.info("请求税务平台推送"+desc+" 地址: " + url);
                logger.info("请求税务平台推送"+desc+" 入参: " + params);
                String res = OkHttpUtils.okHttpPost(url, params);
                logger.info("请求税务平台推送"+desc+" 返参: " + res);
                //生成附件
                JSONObject jsonObject = JSONObject.fromObject(res);
                String code = jsonObject.optString("code");
                if("0".equals(code)){
                    pushtourl(tranTaxHead);
                }
            }
        }
    }
    /**
     * @author: Liujiaji
     * @date: 2024/3/28 09:22
     * @description: TODO 根据id查询采购发票的信息 推送到税务平台
     * @param: []
     * @return: void
     **/
    public void findcgfpbyid(String id){
        if(StringUtils.isEmpty(id)) return;
        List<TranTaxHead> tranTaxHeads = tranTaxMapper.selectTranTaxcgfpByid(id);
        pushtojspt(tranTaxHeads,"采购发票");
    }


    /**
     * @author: Liujiaji
     * @date: 2024/3/27 15:59
     * @description: TODO 应付运费发票推送到税务平台
     * @param: [param]
     * @return: com.ruoyi.common.core.domain.AjaxResult
     **/
    public AjaxResult pushSwTaxyfyf(JSONObject param){
        //应付运费发票根据参数查询erp信息
        JSONArray jsonArray1 = param.optJSONArray("infos");
        String cinvoice_hid = param.optString("cinvoice_hid");
        JSONArray jsonArray = param.optJSONArray("pks");
        if (StringUtils.isNotEmpty(cinvoice_hid)){
            List<TranTaxHead> tranTaxHeads = tranTaxMapper.selectTranTaxByid(cinvoice_hid);
            pushtojspt(tranTaxHeads,"应付运费发票");
        }else if(null != jsonArray1 && jsonArray1.size()>0){
            for (Object o : jsonArray1) {
                JSONObject jsonObject = JSONObject.fromObject(o);
                String id = jsonObject.optString("id");
                //根据id查询运输发票内容并整理为参数
                List<TranTaxHead> tranTaxHeads = tranTaxMapper.selectTranTaxByid(id);
                pushtojspt(tranTaxHeads,"应付运费发票");
            }
        }else if(null != jsonArray && jsonArray.size()>0){
            //批量提交
            for (Object o : jsonArray) {
                String s = String.valueOf(o);
                if(StringUtils.isNotEmpty(s) && s.contains(",")){
                    String[] split = s.split(",");
                    //根据id查询运输发票内容并整理为参数
                    List<TranTaxHead> tranTaxHeads = tranTaxMapper.selectTranTaxByid(split[0]);
                    pushtojspt(tranTaxHeads,"应付运费发票");
                }
            }

        }
        return new AjaxResult(200,"成功");
    }

    /**
     * @author: Liujiaji
     * @date: 2024/3/27 15:54
     * @description: TODO 应付单推送税务平台
     * @param: [param]
     * @return: com.ruoyi.common.core.domain.AjaxResult
     **/
    public AjaxResult pushSwTaxyfd(JSONObject param){
        JSONArray jsonArray = param.optJSONArray("list");
        if(null != jsonArray && jsonArray.size()>0){
            //批量
            for (Object o : jsonArray) {
                JSONObject jsonObject = (JSONObject)(o);
                String billid = jsonObject.optString("pk_bill");
                //根据单据id查询应付单
                List<TranTaxHead> tranTaxHeads = tranTaxMapper.selectTranTaxyfdByid(billid);
                pushtojspt(tranTaxHeads,"应付单管理");
            }
        }else{
            //单个
            String billid = param.optString("pk_bill");
            //根据单据id查询应付单
            List<TranTaxHead> tranTaxHeads = tranTaxMapper.selectTranTaxyfdByid(billid);
            pushtojspt(tranTaxHeads,"应付单管理");
        }
        return new AjaxResult(200,"成功");
    }

    /**
     * @author: Liujiaji
     * @date: 2024/2/28 15:44
     * @description: TODO 新增附件
     * @param: [tranTaxHead]
     * @return: void
     **/
    public void pushtourl(TranTaxHead tranTaxHead){
        String fphmlb = tranTaxHead.getFphmlb();
        if(StringUtils.isNotEmpty(fphmlb)){
            if (fphmlb.contains(",")){
                String[] split = fphmlb.split(",");
                for (String s : split) {
                    tranTaxHead.setFphm(s);
                    geturltojs(tranTaxHead);
                }
            }else{
                tranTaxHead.setFphm(fphmlb);
                geturltojs(tranTaxHead);
            }
        }
    }

    /**
     * @author: Liujiaji
     * @date: 2024/3/28 15:04
     * @description: TODO 请求金税平台获取发票url
     * @param: [tranTaxHead]
     * @return: void
     **/
    public void geturltojs(TranTaxHead tranTaxHead){
        //1.查询url,2.保存url
        String params = JSONObject.fromObject(tranTaxHead).toString();
        String url = sendUrl + "/api/ydcj/invoice/jxInvoiceQueryExt";
        logger.info("请求税务平台获取发票采集池的发票数据 地址: " + url);
        logger.info("请求税务平台获取发票采集池的发票数据接口 入参: " + params);
        String res = OkHttpUtils.okHttpPost(url, params);
        logger.info("请求税务平台获取发票采集池的发票数据接口 返参: " + res);
        JSONObject jsonObject2 = JSONObject.fromObject(res);
        String code = jsonObject2.optString("code");
        if("0".equals(code)){
            JSONArray jsonArray = jsonObject2.optJSONArray("data");
            if(jsonArray.size()>0){
                JSONObject jsonObject = jsonArray.optJSONObject(0);
                String pdfUrl = jsonObject.optString("pdfUrl");
                tranTaxHead.setBz(pdfUrl);
                //请求openapi保存
                savefileurl(tranTaxHead);
            }
        }
    }

    /**
     * @author: Liujiaji
     * @date: 2024/3/28 15:03
     * @description: TODO 请求openapi保存附件url
     * @param: [tranTaxHead]
     * @return: void
     **/
    public void savefileurl(TranTaxHead tranTaxHead){
        JSONObject pjs = new JSONObject();
        pjs.put("billId",tranTaxHead.getFpdid());
        pjs.put("fullPath",tranTaxHead.getFpdid());
        pjs.put("uploadUrl",tranTaxHead.getBz());
        pjs.put("name",tranTaxHead.getFphm()+".url");
        pjs.put("billcode","");
        pjs.put("creator",tranTaxHead.getZdr());
        String params2 = pjs.toString();
        String apiuri2 = "nccloud/api/platform/attachmentuploadurl/baseinfo/uploadfileurl";
        OpenApiUtils.openApiSend(apiuri2, params2,"新增附件url");
    }



}
